473,563 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to assign event to dynamically generated asp:button?

hb
Hi,

When I add an asp:button (ex: id=btnLog) on home.aspx,
I need to create btnLog_Click() event in home.aspx.cs,
and also link this event and the button in OnInit() method
by adding:
this.btnLog.Cli ck +=new System.EventHan dler(this.btnLo g_Click);

Now, I need to generate some asp:button dynamically in
an asp:table, and assign the event to all buttons. But in
the event, I need to retrieve the ID of the clicked button.

Would you please tell me:
1. how can I assign the event to a dynamically generated button?
2. how can I add the link between a specific button and the event in
OnInit()
method?
3. how can I retrieve the ID of the clicked button(i.e. the button
that fires the event)?

Thank you

hb
Nov 18 '05 #1
4 2555
Button btn = new Button();
btn.ID = "firstButto n";
btn.Click += new System.EventHan dler(this.btnLo g_Click);
SomeParentContr ol.Controls.Add (btn);

btn = new Button();
btn.ID = "second";
btn.Click += new System.EventHan dler(this.btnLo g_Click);
SomeParentContr ol.Controls.Add (btn);

public void btnLog_Click(ob ject sender, EventArgs e) {
string id = ((Button)sender ).ID;
}
The controls need to be created on postback as well, so no wrapping it in a
if (!Page.IsPostBa ck) {}

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"hb" <ho****@goodoff ices.com> wrote in message
news:us******** ******@TK2MSFTN GP11.phx.gbl...
Hi,

When I add an asp:button (ex: id=btnLog) on home.aspx,
I need to create btnLog_Click() event in home.aspx.cs,
and also link this event and the button in OnInit() method
by adding:
this.btnLog.Cli ck +=new System.EventHan dler(this.btnLo g_Click);

Now, I need to generate some asp:button dynamically in
an asp:table, and assign the event to all buttons. But in
the event, I need to retrieve the ID of the clicked button.

Would you please tell me:
1. how can I assign the event to a dynamically generated button?
2. how can I add the link between a specific button and the event in
OnInit()
method?
3. how can I retrieve the ID of the clicked button(i.e. the button
that fires the event)?

Thank you

hb

Nov 18 '05 #2
> 1. how can I assign the event to a dynamically generated button?

OnInit Event of form add

Button btn1 = new Button();
btn1.ID = "Test";
btn1.Click += new EventHandler(My ButtonClick);
LocateForm(this ,btn1); -- FUNCTION TO ADD THE BUTTON

private void LocateForm(Cont rol ctrl, Button btn){
foreach(Control mCtrl in ctrl.Controls){
if (mCtrl.GetType( ) == typeof(HtmlForm )){
mCtrl.Controls. Add(btn);
}else{
LocateForm(mCtr l,btn);
}
}
}

private void MyButtonClick(o bject sender, System.EventArg s e){
// DO SOMETHING
}

3. how can I retrieve the ID of the clicked button(i.e. the button
that fires the event)?


On The Previous function
private void MyButtonClick(o bject sender, System.EventArg s e){
string buttonID = ((Button)sender ).ID;
// DO SOMETHING
}
Nov 18 '05 #3
hb
Hi, Karl,

It works! Thank you for the help!

HB
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OV******** ******@TK2MSFTN GP09.phx.gbl...
Button btn = new Button();
btn.ID = "firstButto n";
btn.Click += new System.EventHan dler(this.btnLo g_Click);
SomeParentContr ol.Controls.Add (btn);

btn = new Button();
btn.ID = "second";
btn.Click += new System.EventHan dler(this.btnLo g_Click);
SomeParentContr ol.Controls.Add (btn);

public void btnLog_Click(ob ject sender, EventArgs e) {
string id = ((Button)sender ).ID;
}
The controls need to be created on postback as well, so no wrapping it in a if (!Page.IsPostBa ck) {}

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"hb" <ho****@goodoff ices.com> wrote in message
news:us******** ******@TK2MSFTN GP11.phx.gbl...
Hi,

When I add an asp:button (ex: id=btnLog) on home.aspx,
I need to create btnLog_Click() event in home.aspx.cs,
and also link this event and the button in OnInit() method
by adding:
this.btnLog.Cli ck +=new System.EventHan dler(this.btnLo g_Click);

Now, I need to generate some asp:button dynamically in
an asp:table, and assign the event to all buttons. But in
the event, I need to retrieve the ID of the clicked button.

Would you please tell me:
1. how can I assign the event to a dynamically generated button?
2. how can I add the link between a specific button and the event in
OnInit()
method?
3. how can I retrieve the ID of the clicked button(i.e. the button
that fires the event)?

Thank you

hb


Nov 18 '05 #4
hb
Hi, Amar,

It works! Thank you for your help!

hb
"Amar" <am******@yahoo .com> wrote in message
news:7c******** *************** ***@posting.goo gle.com...
1. how can I assign the event to a dynamically generated button?


OnInit Event of form add

Button btn1 = new Button();
btn1.ID = "Test";
btn1.Click += new EventHandler(My ButtonClick);
LocateForm(this ,btn1); -- FUNCTION TO ADD THE BUTTON

private void LocateForm(Cont rol ctrl, Button btn){
foreach(Control mCtrl in ctrl.Controls){
if (mCtrl.GetType( ) == typeof(HtmlForm )){
mCtrl.Controls. Add(btn);
}else{
LocateForm(mCtr l,btn);
}
}
}

private void MyButtonClick(o bject sender, System.EventArg s e){
// DO SOMETHING
}

3. how can I retrieve the ID of the clicked button(i.e. the button
that fires the event)?


On The Previous function
private void MyButtonClick(o bject sender, System.EventArg s e){
string buttonID = ((Button)sender ).ID;
// DO SOMETHING
}

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4645
by: Frag | last post by:
Hi guys, I searched around without any clear answer. Tons of stuff, but nothing concrete. I am trying to call this Javascript function: function ButtonClicked() { alert("The button has been clicked."); }
4
2984
by: joe.osowski | last post by:
I've been staring at this a while, and I haven't had much luck with it. So I figure I'll try Usenet. No matter what I try, it seems the event.button property is always "0". Here is my test case HTML, can anyone tell me what I am doing wrong? Thanks in advance.
2
5007
by: Prisy | last post by:
Is it possible to have the text of the first-letter in an <asp:button> underlined? I was able to do the following to get the first letter underlined: <STYLE type="text/css"> BUTTON:first-letter { TEXT-DECORATION: underline } </STYLE> <button id="btnTest" accesskey="T" runat="server" type="button" value="Test" Test</button>
2
2114
by: giant food | last post by:
Hi, I'm writing an asp app. I have a text box with a validator and a submit button. Here is code from my .aspx file.... <asp:TextBox ID="txName" TextMode="SingleLine" Runat="server" /> <asp:RequiredFieldValidator ID="vName" Runat=server ControlToValidate="txName" /> <asp:Button ID="btnSave" CssClass="button" Runat="server" />
4
2276
by: z. f. | last post by:
Hi, I'm having an aspx page with a server form. i have a grid with a delete button and below the grid, another area with inputs for inserting new values and an "add" button for submiting the lower area of the form. on the lower area i have validators for validating input.
4
1268
by: Tim Wallace | last post by:
I have an ascx file in which I want to sometimes create an asp:button control based on a given value. Nothing I've tried has worked. How can I accomplish this task? Tim
0
1641
by: enantiomer | last post by:
Having a problem with visual studio 2005 doing ASP.net. when in source mode (i.e. looking at the html directly), in the properties toolbar is an option called "DOCUMENT". It has some basic information like culture and title. From what I remember, when you switch to design view though, it should present an option in the properties toolbar...
1
3428
by: FredZimmerman | last post by:
How do I dynamically generate <ASP:buttoncontrols for a repeating recordset (using reader with while loop). Each row would have ASP:Button and CommandName="sameFunctionForAll()" CommandArgument=reader.ToString() I'm not at all sure how to tackle this. I have page that contains embedded .ASCX. The buttons would be displayed within the...
4
1633
vavc1980
by: vavc1980 | last post by:
hello! I have a form in a aspx file, in which I have a button (asp:button), I want to add/attach an event (onclick, or onsubmit) using javascript, I know I just can type the syntax right there, but I was requested to do it through javascript document.getelementbyid. Is that possible? if so, how? Thanks
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.