468,306 Members | 1,197 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

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 8244
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fredrik Rodin | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.