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

Introduction:
In this article I will explain how to implement dynamic button click events in asp.net using c# or vb.net.
Description:

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>
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)
{
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");
}
VB.NET Code


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
For more details check below posts


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

3 comments :

kavinraj said...

hi suresh its very good experience by learning your blog..can u post any article reg Globalisation and Localisation if you have

athul dk said...

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
}

Rajkumar Palnati said...

Hi,

HtmlImage image = new HtmlImage();

for this html control how can i create the event handler dynamically ?

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.