473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.Click +=new System.EventHandler(this.btnLog_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 2546
Button btn = new Button();
btn.ID = "firstButton";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

btn = new Button();
btn.ID = "second";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

public void btnLog_Click(object 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.IsPostBack) {}

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"hb" <ho****@goodoffices.com> wrote in message
news:us**************@TK2MSFTNGP11.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.Click +=new System.EventHandler(this.btnLog_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(MyButtonClick);
LocateForm(this,btn1); -- FUNCTION TO ADD THE BUTTON

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

private void MyButtonClick(object sender, System.EventArgs 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(object sender, System.EventArgs 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**************@TK2MSFTNGP09.phx.gbl...
Button btn = new Button();
btn.ID = "firstButton";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

btn = new Button();
btn.ID = "second";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

public void btnLog_Click(object 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.IsPostBack) {}

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"hb" <ho****@goodoffices.com> wrote in message
news:us**************@TK2MSFTNGP11.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.Click +=new System.EventHandler(this.btnLog_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.google.c om...
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(MyButtonClick);
LocateForm(this,btn1); -- FUNCTION TO ADD THE BUTTON

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

private void MyButtonClick(object sender, System.EventArgs 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(object sender, System.EventArgs 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
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...
4
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...
2
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">...
2
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" />...
4
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...
4
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
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...
1
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()"...
4
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.