Handling events from dynamically created link buttons

M

Mark

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
 
M

Mark Rae

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top