Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Dynamic LinkButton Controls in Asp.Net

Introduction

In this post I will explain how to create and add dynamic table with dynamic link button in asp.net code behind. My previous post Error Logging in C# Asp.net.

Explanation

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.

Coding
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)
{
If(!IsPostBack())
{
}
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();
SqlDap.Fill(objDT);

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);
tblCellDef.Controls.Add(lnkEmployee);
tblRowDef.Controls.Add(tblCellDef);
intCurrentCount += 1;
}
}
}
tblDynamic.Controls.Add(tblRowDef);
}

}
pnlDynamic.Controls.Add(tblDynamic);
}
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;
Response.Redirect("SomePage.aspx?ID="+lnkEmployee.CommandName);
}

Output
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.My previous post Error Logging in C# Asp.net.


This post first appeared on Dotnet, please read the originial post: here

Share the post

Dynamic LinkButton Controls in Asp.Net

×

Subscribe to Dotnet

Get updates delivered right to your inbox!

Thank you for your subscription

×