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.
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>
|
using System;
using
System.Web.UI.WebControls;
|
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);
}
|
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
|
|
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 Email
|
|||
|
|
|

Subscribe by RSS
Subscribe by Email
10 comments :
I liked it.......
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.
Gud.... I like it
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...
we also can paste this code behind page load part in page_Init() event
somethings
your blog is very useful for me in all ways thanks thanks a lot
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.
How to remove dynamically created controls..?
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.