Dynamic LinkButton


Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

Dynamic LinkButton

On this page we'll look at how to dynamically create an ASP.NET LinkButton using C#.

The LinkButton appears like a standard anchor link in HTML, but instead of processing a Http GET request, which may involve passing parameters in the querystring, a LinkButton acts like an ASP.NET Button and produces a PostBack, or a Http POST, with parameters included in the HTTP message, so they are not visible in the querystring. So there maybe times when we want to use a LinkButton. There may also be times when we don't know how many LinkButtons we need, we could be building a list of options available to the user, and therefore we may have an unknown number of LinkButtons to create, so may have to create them dynamically.

Dynamically creating a LinkButton

If we dynamically create LinkButtons, then we can do this in the code behind file. We need to create a new instance of a LinkButton, assign properties which we need. We need to assign an event handler for it's Click event, and then finally add the newly created LinkButton to the page.

The following code example shows how to create a LinkButton dynamically in the Page_Load event:


protected void Page_Load(object sender, EventArgs e)
{
     LinkButton linkButton = new LinkButton();
     linkButton.Text = "The Button";
     linkButton.CausesValidation = false;
     linkButton.CommandArgument = "Red";
     linkButton.CommandName = "Colour";
     linkButton.Command += new CommandEventHandler(this.OnDynamicLinkButtonClick);
     PlaceHolder_Buttons.Controls.Add(linkButton);
}

void OnDynamicLinkButtonClick(object sender, CommandEventArgs e)
{
    LinkButton clickedButton = (LinkButton)sender;
    String commandName = clickedButton.CommandName;
    String commandArgument = clickedButton.CommandArgument;
}


In the ASPX page, we have a PlaceHolder which is where we are going to add our dynamically created LinkButton.


<asp:PlaceHolder ID="PlaceHolder_Buttons" runat="server" />


So as you can seem we can assign propertied to the LinkButton such as it's Text, whether it fires off Validation, we can give the Button values for CommandArgument and CommandName, data which can be useful for the Click event handler, so in the Click event you can work out exactly what action you should be taking. For instance, if you needed to create 10 new LinkButtons, each to represent different colours, in the Click event handler you tell which colour they have picked by looking at the CommandArgument property.

Having both CommandName and CommandArgument to be populated allows quite alot of flexibility, and means we can use the same Click event handler for each of our dynamically created LinkButtons, instead of having multiple methods. This is useful if each button pretty much does the same thing, except for a minor difference, such as using a different colour, then using the same event handler for each button, instead of writing one for each button, means in this case we have 1 method instead of 10.