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

Dynamically create controls in handle button click events

Jul 29, 2012

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


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

<html xmlns="">
<head id="Head1" runat="server">
<title>Create controls dynamically in and handle button click events</title>
<form id="form2" runat="server">
<asp:Panel ID="pnlInfo" runat="server">
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:";
TextBox txtName=new TextBox();
txtName.ID = "txtName";
tc=new TableCell();
tc1=new TableCell();
tr=new TableRow();
Label lblEmail = new Label();
lblEmail.ID = "lblEmail";
lblEmail.Text = "Email:";
TextBox txtEmail = new TextBox();
txtEmail.ID = "txtEmail";
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);
// 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:"
Dim txtName As New TextBox()
txtName.ID = "txtName"
tc = New TableCell()
tc1 = New TableCell()
tr = New TableRow()
Dim lblEmail As New Label()
lblEmail.ID = "lblEmail"
lblEmail.Text = "Email:"
Dim txtEmail As New TextBox()
txtEmail.ID = "txtEmail"
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
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,, 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


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...


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

powerful peraiah said...

HI suresh, When i was clicking the dynamically created button it was not fired can u please suggest me for that....

Give your Valuable Comments

Other Related Posts

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