473,772 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8526
"Mark" <ma*********@no spam.nospamwrot e in message
news:OF******** ******@TK2MSFTN GP02.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(objec t sender, System.EventArg s e)
{
LinkButton MyLinkButton;

foreach(<number of buttons to create)
{
MyLinkButton = new LinkButton();
MyLinkButton.En ableViewState = true;
MyLinkButton.ID = "MyLinkButt on _" + <unique identifier>;
MyLinkButton.Te xt = "<some text to display>";
MyLinkButton.Co mmand += new CommandEventHan dler(MyLinkButt on
_Command);
MyLinkButton.Co mmandName = <unique identifier>;
MyLinkButton.Vi sible = false;
this.Controls.A dd(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, CommandEventArg s 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**@markNOSPA Mrae.comwrote in message
news:O1******** ******@TK2MSFTN GP03.phx.gbl...
"Mark" <ma*********@no spam.nospamwrot e in message
news:OF******** ******@TK2MSFTN GP02.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(objec t sender, System.EventArg s e)
{
LinkButton MyLinkButton;

foreach(<number of buttons to create)
{
MyLinkButton = new LinkButton();
MyLinkButton.En ableViewState = true;
MyLinkButton.ID = "MyLinkButt on _" + <unique identifier>;
MyLinkButton.Te xt = "<some text to display>";
MyLinkButton.Co mmand += new CommandEventHan dler(MyLinkButt on
_Command);
MyLinkButton.Co mmandName = <unique identifier>;
MyLinkButton.Vi sible = false;
this.Controls.A dd(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, CommandEventArg s e)
{
// identify which button was clicked by e.CommandName;
// do something
}

Jan 8 '07 #3
"Mark" <ma*********@no spam.nospamwrot e in message
news:Op******** *****@TK2MSFTNG P06.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
2529
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='" & rdrOnet3("ONET3_TITLE") & "' CommandArgument='" & rdrOnet3("ONET3_CD") & "' id='lb" & i & "'></asp:LinkButton>"
2
1156
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 lnkbtn_grp1 As New LinkButton
1
2589
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 LinkButton myLB = new LinkButton(); ......... .........
5
6762
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 link buttons to stay wired up to their click events. I have what is basically a simply survey question generation page. The page first displays a few static fields and a dropdownlist of various options for the user to select. When the user...
1
2284
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function //////////////////////////////////////////////////////////////////////////////////// version 1 : using global variables ////////////////////////////////////////////////////////////////// var _textField, _divColorPicker; // global vars window.onload = function() { _textField = document.getElementById('htxaMessage'); // fill...
1
2226
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(); b.Text = str_user_names; b.Size = new Size(150, 75); Font boldfont = new Font("Times new roman", 16.0F, FontStyle.Bold); b.Font = boldfont; ...
7
4875
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 buttons and check box. I am adding two radio buttons once for a row.Now my problem is the radio buttons in all the rows that added dynamically are behaving as same group i.e when i am selecting a radio button in second row,radio button in first row...
5
6704
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 = ConfigurationManager.ConnectionStrings.ConnectionString; protected void Page_Load(object sender, EventArgs e) { try
1
3078
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 able to open each of the file by clicking it. i have succeeded in displaying the results in a dynamic table. I am using link buttons to open the files.when i click the button there is no action. my code namespace WebApplication3 { ...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6713
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.