473,394 Members | 1,755 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,394 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 8499
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.