473,233 Members | 1,556 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,233 software developers and data experts.

Handling events from dynamically created link buttons

Assume you want to dynamically add one to many link button controls to a web
page dynamically at run time. Each link button needs to post back and
execute code. As the link buttons are created at run time and the number may
vary, we can't statically tie to the event to a control. I'm assuming I
need to dynamically add event handlers at run time, and that one method
could act as the event handler, with unique event args being created for
each link button? What is the best way to approach this? Thanks in
advance.

Mark
Jan 8 '07 #1
3 8492
"Mark" <ma*********@nospam.nospamwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
Assume you want to dynamically add one to many link button controls to a
web page dynamically at run time. Each link button needs to post back and
execute code. As the link buttons are created at run time and the number
may vary, we can't statically tie to the event to a control. I'm assuming
I need to dynamically add event handlers at run time, and that one method
could act as the event handler, with unique event args being created for
each link button? What is the best way to approach this?
If your source data is being pumped into any sort of bound control e.g. a
GridView or a Repeater, you don't need to worry about this. However, if it's
not, then as you say you will need to create the buttons manually.

The first thing is that you need to create the dynamic controls in the
Page_Init event - you don't have to do anything with them at this stage if
you don't want to, but you have to create them here. OK - that's not
"strictly" true - you can create them in other events, but if you create
them in Page_Init you can pretty much guarantee that everything will work as
you expect.

Secondly, to create the controls themselves is fairly simple:

private void Page_Init(object sender, System.EventArgs e)
{
LinkButton MyLinkButton;

foreach(<number of buttons to create)
{
MyLinkButton = new LinkButton();
MyLinkButton.EnableViewState = true;
MyLinkButton.ID = "MyLinkButton _" + <unique identifier>;
MyLinkButton.Text = "<some text to display>";
MyLinkButton.Command += new CommandEventHandler(MyLinkButton
_Command);
MyLinkButton.CommandName = <unique identifier>;
MyLinkButton.Visible = false;
this.Controls.Add(MyLinkButton);
}
}

That creates as many different buttons as you need, and adds them to the
page's Control collection so that you can drop them into your page later on
in the page cycle, probably in Page_Load...

When any of the buttons is clicked, it will fire the following event:

private void MyLinkButton _Command(object sender, CommandEventArgs e)
{
// identify which button was clicked by e.CommandName;
// do something
}
Jan 8 '07 #2
You're a rock star. Thanks for the help! The details are deeply
appreciated.

Mark
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
"Mark" <ma*********@nospam.nospamwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
>Assume you want to dynamically add one to many link button controls to a
web page dynamically at run time. Each link button needs to post back
and execute code. As the link buttons are created at run time and the
number may vary, we can't statically tie to the event to a control. I'm
assuming I need to dynamically add event handlers at run time, and that
one method could act as the event handler, with unique event args being
created for each link button? What is the best way to approach this?

If your source data is being pumped into any sort of bound control e.g. a
GridView or a Repeater, you don't need to worry about this. However, if
it's not, then as you say you will need to create the buttons manually.

The first thing is that you need to create the dynamic controls in the
Page_Init event - you don't have to do anything with them at this stage if
you don't want to, but you have to create them here. OK - that's not
"strictly" true - you can create them in other events, but if you create
them in Page_Init you can pretty much guarantee that everything will work
as you expect.

Secondly, to create the controls themselves is fairly simple:

private void Page_Init(object sender, System.EventArgs e)
{
LinkButton MyLinkButton;

foreach(<number of buttons to create)
{
MyLinkButton = new LinkButton();
MyLinkButton.EnableViewState = true;
MyLinkButton.ID = "MyLinkButton _" + <unique identifier>;
MyLinkButton.Text = "<some text to display>";
MyLinkButton.Command += new CommandEventHandler(MyLinkButton
_Command);
MyLinkButton.CommandName = <unique identifier>;
MyLinkButton.Visible = false;
this.Controls.Add(MyLinkButton);
}
}

That creates as many different buttons as you need, and adds them to the
page's Control collection so that you can drop them into your page later
on in the page cycle, probably in Page_Load...

When any of the buttons is clicked, it will fire the following event:

private void MyLinkButton _Command(object sender, CommandEventArgs e)
{
// identify which button was clicked by e.CommandName;
// do something
}

Jan 8 '07 #3
"Mark" <ma*********@nospam.nospamwrote in message
news:Op*************@TK2MSFTNGP06.phx.gbl...
You're a rock star.
Occasionally...
Jan 8 '07 #4

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

Similar topics

0
by: Burak | last post by:
Hello, This is my code behind code for dynamic link button creation: strUrl = "<asp:LinkButton CommandName='Onet3_Cd' onfiltered='LinkButton_Command' runat='server' " & _ "Text='" &...
2
by: Fredrik Rodin | last post by:
All, I have a sub that generates a number of link buttons depending on the number of records returned by the SQL. Do While dReader.Read Dim tr As New TableRow Dim td As New TableCell Dim...
1
by: Gopal Krish | last post by:
I'm have coded a simple menu (using link buttons as menu items) in a user control to be reused across many ASPX pages. In the page_load method I dynamically create the link buttons as follows ...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
1
by: hyerk | last post by:
I currently have a FlowLayoutPanel that creates buttons dynamically for each entry in a database: for (int i = 0; i < int_num_users; i++) { Button b = new Button(); ...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
5
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
1
by: Sujeetharan | last post by:
I am writing a code for a search function. I need to search the folders for the particular file/files(only excel files/ there may be several files with the same name) & display them. I should also be...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.