Dynamic LinkButton Controls in Asp.Net

In this article am going to explain how to create and add dynamic table with Dynamic LinkButton.

In the design page, we just add how many controls we need and where it should be placed. These controls are static and having the data what we gave. In some cases we have to dynamically get data from the database and create dynamic controls for the data.

Difference between static and dynamic?
Static -> we can mention how many things we need.
Dynamic ->we cannot mention it generates depends upon the requirements.

Creating Dynamic LinkButton in Asp.Net is very simple. After creating the dynamic controls just add the controls in a PlaceHolder or Panel control.

Step 1: In the Design Page just create a panel named “pnlDynamic”.

Step 2: Add the namespace in the code behind.

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

Step 3: Create an object for HtmlTable.

HtmlTable tblDynamic = new HtmlTable();

Step 4: In the Page_Load event, just call the dynamically creating control method BindEmployeeData(); Call the method outside the IsPostBack() method. Because our dynamic controls should recreate each and every time when a postback call.

protected void Page_Load(object sender, EventArgs e)
    BindEmployeeData ();

Step 5: Method for creating the dynamic LinkButton control depends on the database data.

public void BindEmployeeData()
      SqlConnection SqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=mytestDB;User ID=xxxxxxx;Password=zzzzzzzzz");
      SqlDataAdapter SqlDap = new SqlDataAdapter("select * from Employees", SqlConn);
      DataTable objDT = new DataTable();

      tblDynamic.ID = "tblEmployee";
      tblDynamic.Width = "100%";
      tblDynamic.CellPadding = 3;
      int intRowCount = objDT.Rows.Count;

      if ((intRowCount > 0))
          int sCount = (intRowCount / 2) + 1;
          int intCurrentCount = 0;
          for (int i = 1; i <= sCount; i++)                 {                     HtmlTableRow tblRowDef = new HtmlTableRow();                                          tblRowDef.Align = "left";                                          if ((intRowCount > intCurrentCount))
                  for (int l = 1; l <= 2; l++)                         {                             if ((intRowCount > intCurrentCount))
                          HtmlTableCell tblCellDef = new HtmlTableCell();
                          tblCellDef.ID = "0" + objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          tblCellDef.Width = "400px";
                          LinkButton lnkEmployee = new LinkButton();
                          lnkEmployee.ID = "lnkEmployee0" + objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          lnkEmployee.Text = objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          lnkEmployee.CommandName = objDT.Rows[intCurrentCount]["EmpID"].ToString();
                          lnkEmployee.Font.Bold = true;
                          lnkEmployee.Font.Size = 13;
                          lnkEmployee.Width = 10;
                          lnkEmployee.Click += new EventHandler(lnkEmployeeWhole_Click);
                          intCurrentCount += 1;


Step 6: Write the handler to fire the event for the particular link button control

protected void lnkEmployeeWhole_Click(object sender, EventArgs e)
      LinkButton lnkEmployee = (LinkButton)sender;

Dynamic link button will disappear if you put LoadBookData() method within IspostBack() method.

I Hope these Code will help you to understand the Dynamic LinkButton Controls in Asp.Net.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s