Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Create Controls Dynamically in code behind files using asp.net

Sep 26, 2010
Introduction:

Here I will explain how to generate the controls dynamically in code behind using asp.net

Description:

Hi here I will explain how to generate the controls dynamically in asp.net code behind
First design the your aspx page with one panel like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>

After that write the following code in page load method like this 


protected void Page_Load(object sender, EventArgs e)
{
Table tbldynamic = new Table();
for (int i = 0; i < 5; i++)
{
TableCell tc = new TableCell();
TableRow tr = new TableRow();
CheckBox _checkbox = new CheckBox();
_checkbox.ID = "chkDynamicCheckBox" + i;
_checkbox.Text = "Checkbox" + i;
tc.Controls.Add(_checkbox);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
}
Panel1.Controls.Add(tbldynamic);
}

Now run application you will see dynamically generated checkboxes like that we can generate controls dynamically.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

18 comments :

harikishore said...

nice and excellent articles

Anonymous said...

Hi,

How to write "CheckedChanged" Event for each CheckBox.

Anonymous said...

can u explain how to work with events of dynamically created controls like managing ControlId,events of dynamic control

Anonymous said...

Suresh Please reply to the above comments 2 and 3.

Anonymous said...

Hope this works!!!!! :)

Button btnDyn = new Button();
btnDyn.ID = "btnDyn";
btnDyn.Style["Position"] = "Absolute";
btnDyn.Style["Top"] = "100px"; btnDyn.Style["Left"] = "10px";
btnDyn.Click += new EventHandler(Button_Click);
this.form1.Controls.Add(btnDyn);

Anonymous said...

nice site and excellent tutorials...

shawn said...

But How to find that checkbox ////
when we going to click a button to get value of selected check boxes

gaurav mamidwar said...

Thanx for good guidence.....

mohit srivastava said...

hi suresh you are rock man.
can you help me to make a project like book a seat
where i create dynamic seats(img btn)
and make event on all of it

Anonymous said...

I am not able to Print the dynamic table ....
plz help me

regards
sanjay

Anonymous said...

dynamic table with label and textboxs
plz help me


sarita khaire said...

I have created one dynamic table .and this table contains Button in each row.I want to access value from each cell on button click event ..How can i do plz give me explain..

Anonymous said...

what about radiobuttons list

mohammad noorani said...

hi thank you for your helpful Article
i have a problem with this
when i want to add text and control in one tablecell just return me one of them
what should i do?

yogesh niranjan said...

Hi Suresh, you are rock, your code will help us in many critical situation.

Thanks a lot.

Anonymous said...

Superb and Simple implementation........

Anonymous said...

Hello,
its good article...but how to retrieve the same dunamically created html controls values in a button click.

bhavesh thumar said...

Hello dear

i have to create drag and dropable control in asp.net like visual studio online i can drag my panel please tell me some idea about them..

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.