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

Dynamically create controls in asp.net handle button click events

Jul 29, 2012
Introduction:

In this article I will explain how to create dynamic controls like button, linkbutton and other controls and how to handle button click events in asp.net.

Description:

In previous posts I explained how to create controls dynamically in asp.net and many articles relating to
Asp.net, JQuery, and SQL etc. Now I will explain how to handle dynamically created controls click events in asp.net for that first we need to write the following code in your aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Create controls dynamically in asp.net and handle button click events</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Panel ID="pnlInfo" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
After completion of aspx page design add the following namespaces in code behind

C# code


using System;
using System.Web.UI.WebControls;
After add namespaces write the following code in code behind like as shown below


protected void Page_Load(object sender, EventArgs e)
{
Table tbldynamic = new Table();
TableCell tc=new TableCell();
TableCell tc1=new TableCell();
TableRow tr = new TableRow();
Label lblName =new Label();
lblName.ID = "lblName";
lblName.Text = "UserName:";
tc.Controls.Add(lblName);
TextBox txtName=new TextBox();
txtName.ID = "txtName";
tc1.Controls.Add(txtName);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
tc=new TableCell();
tc1=new TableCell();
tr=new TableRow();
Label lblEmail = new Label();
lblEmail.ID = "lblEmail";
lblEmail.Text = "Email:";
tc.Controls.Add(lblEmail);
TextBox txtEmail = new TextBox();
txtEmail.ID = "txtEmail";
tc1.Controls.Add(txtEmail);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
tc = new TableCell();
tc1 = new TableCell();
tr = new TableRow();
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Submit";
btnSubmit.Click += new System.EventHandler(btnSubmit_click);
tc1.Controls.Add(btnSubmit);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
pnlInfo.Controls.Add(tbldynamic);
}
// Dynamic button click event
protected void btnSubmit_click(object sender, EventArgs e)
{
TextBox txtUserName = (TextBox)pnlInfo.FindControl("txtName");
TextBox txtEmail = (TextBox)pnlInfo.FindControl("txtEmail");
Response.Write("UserName: "+txtUserName.Text+"; "+"Email: "+txtEmail.Text);
}
VB.NET Code

Imports System.Web.UI.WebControls
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim tbldynamic As New Table()
Dim tc As New TableCell()
Dim tc1 As New TableCell()
Dim tr As New TableRow()
Dim lblName As New Label()
lblName.ID = "lblName"
lblName.Text = "UserName:"
tc.Controls.Add(lblName)
Dim txtName As New TextBox()
txtName.ID = "txtName"
tc1.Controls.Add(txtName)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
tc = New TableCell()
tc1 = New TableCell()
tr = New TableRow()
Dim lblEmail As New Label()
lblEmail.ID = "lblEmail"
lblEmail.Text = "Email:"
tc.Controls.Add(lblEmail)
Dim txtEmail As New TextBox()
txtEmail.ID = "txtEmail"
tc1.Controls.Add(txtEmail)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
tc = New TableCell()
tc1 = New TableCell()
tr = New TableRow()
Dim btnSubmit As New Button()
btnSubmit.ID = "btnSubmit"
btnSubmit.Text = "Submit"
AddHandler btnSubmit.Click, AddressOf btnSubmit_click
tc1.Controls.Add(btnSubmit)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
pnlInfo.Controls.Add(tbldynamic)
End Sub
' Dynamic button click event
Protected Sub btnSubmit_click(ByVal sender As Object, ByVal e As EventArgs)
Dim txtUserName As TextBox = DirectCast(pnlInfo.FindControl("txtName"), TextBox)
Dim txtEmail As TextBox = DirectCast(pnlInfo.FindControl("txtEmail"), TextBox)
Response.Write(("UserName: " + txtUserName.Text & "; " & "Email: ") + txtEmail.Text)
End Sub
End Class
Demo


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

13 comments :

Manoj said...

I liked it.......

nick said...

its very helpful topic for me. thanx. where can i find some more detail about dynamic control. like binding database with app. etc. if you have related topic about it plz tell me.

venu chereddy said...

Gud.... I like it

Jagadeesh Naidu said...

Hi suresh...Your blog is excellent...
How to add variable number of textboxes to datatable rows, when clicked on add button and delete the rows one by one when clicked on delete button and finally save the values into database.How to do that.Please help me.Its very urgent in my Project.I searched on many sites but i didn't get the solution...

Anonymous said...

we also can paste this code behind page load part in page_Init() event

Anonymous said...

somethings

Anonymous said...

your blog is very useful for me in all ways thanks thanks a lot

Naga Lakshmi said...

sir i need the requirement like onclick of the button need to generate two more textboxes and need to store in the data base can you please write the article for this.

Anonymous said...

How to remove dynamically created controls..?

Akash Soni said...

hi i have four cascading dropdown list and four buttons for adding item to dropdownlist i have added four textbox ,the visiblity of textbox is false on pageload event and i made it visible on button click event ,the button click event works well when item is not selected in dropdowlist but item is selected button doesnot work please help me in solving out issue.

Anonymous said...

Great Coding Skills you have... Keep Learning and Keep Leading...

Anonymous said...

it's really works.. thanx for your skill....

dinesh j Chandran said...

I Want to create a dynamic USERcontrol based on the XML file?

Which will be the best way to proceed with ? Plz Someone help me with this

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.