In previous posts I explained how to create controls dynamically in asp.net and save dynamic control values in database and many articles relating to Asp.net, JQuery, and SQL etc. Now I will explain how to handle dynamic button click events in asp.net using c# or vb.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)
{
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Submit";
btnSubmit.Click += new
System.EventHandler(btnSubmit_click);
pnlInfo.Controls.Add(btnSubmit);
}
// Dynamic button click event
protected void btnSubmit_click(object sender, EventArgs
e)
{
Response.Write("you
clicked on button");
}
|
Imports System.Web.UI.WebControls
Partial Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles
Me.Load
Dim btnSubmit As New Button()
btnSubmit.ID = "btnSubmit"
btnSubmit.Text = "Submit"
AddHandler btnSubmit.Click, AddressOf
btnSubmit_click
pnlInfo.Controls.Add(btnSubmit)
End Sub
' Dynamic button click event
Protected Sub btnSubmit_click(ByVal sender As Object, ByVal e As EventArgs)
Response.Write("you
clicked on button")
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
2 comments :
hi suresh its very good experience by learning your blog..can u post any article reg Globalisation and Localisation if you have
Hello, i created a dynamic button and a click event,its working, but on that click event i created one more button and its event is not working. Please help me(athuldk@gmail.com)
void DynamicButton()
{
//Button1 is created
clickevent1 is created
}
clickevent_Click
{
//Button2 is created
//clickevent 2 is created
}
clickEvent2_Click
{
//NOT WORKING
}