How to create link button control in run time using C# asp.net
Create
Link button dynamically in asp.net c#
In this article we are going to look into how to create
dynamic control in asp.net C# application. In project sometimes we may come up with a requirement to
create control in runtime. There might be situation where we want to create
link button according to stored data in database. As we don’t know how much
data has stored in database we cannot create control in design time. So we have
to create control in run time.
Step 1) Create aspx page like below.
<%@
Page Language="C#" AutoEventWireup="true" CodeBehind="LinkTest.aspx.cs" Inherits="ASP.NET.LinkTest"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
This aspx page is very simple with
two controls in design time. One label to display text and one placeholder
control to keep all control which will create in runtime.
Step 2) Add below C# code in your .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ASP.NET
{
public partial class LinkTest : System.Web.UI.Page
{
private void CreateControl()
{
Int32 i; //create a integer variable
for(i = 0; i < 5; i++) // will generate 10 LinkButton
{
//create instance of LinkButton
LinkButton lb = new LinkButton();
lb = new LinkButton();
lb.Text = Convert.ToString(i) + " "; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); //LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); //LinkButton CommanName
lb.Command += new CommandEventHandler(lb_Command);//Create Handler for it.
//type
lb.Command += and press double time Tab Key it will generat the lb_Command()
code
PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
}
void lb_Command(object sender, CommandEventArgs e)
{
LinkButton lnk = sender as LinkButton;
Label1.Text = e.CommandName; // will display the which Linkbutton clicked
if(lnk.Font.Bold == true)
{
lnk.Font.Bold = false;
lnk.ForeColor = System.Drawing.Color.Blue;
}
else
{
lnk.Font.Bold = true;
lnk.ForeColor = System.Drawing.Color.Green;
}
}
protected void Page_Load(object sender, EventArgs e)
{
CreateControl();
}
}
}
In page load event , CreateControl() is called and in CreateControl() function for loop we are creating 5 link buttons. We are assigning button property within
for loop.
And by below line
lb.Command += new CommandEventHandler(lb_Command);
we are initializing event handler for each and every
link button. Now, If you go in event handler you will find code
to make button text bold and normal after every click.
If you want to decorate each link button after every
click simply you have to make change in event handeler code.

Conclusion :
This is simple example to create link button dynamically . You can create any control in same way