DoughBoy
asked on
Many controls of same type using one common event in codebehind
Hi All,
I would like to have multiple bottons on one ASPX form. As well, very similar activity would happen during the onclick event of the buttons. I am much too lazy to retype the same code for each of the onclick events. How can I use the same onlick event for all of my buttons and just pass a parameter to the onclick event?
So, for example, I will have the following in the codebehind:
protected System.Web.UI.WebControls. Button Button1;
protected System.Web.UI.WebControls. Button Button2;
protected System.Web.UI.WebControls. Button Button3;
private void ButtonX_Click(object sender, System.EventArgs e)
{
//send additional parameter value A, B, or C, depending on the button
......
//call next function...
}
What code adjustments should I made?
TIA,
DoughBoy
I would like to have multiple bottons on one ASPX form. As well, very similar activity would happen during the onclick event of the buttons. I am much too lazy to retype the same code for each of the onclick events. How can I use the same onlick event for all of my buttons and just pass a parameter to the onclick event?
So, for example, I will have the following in the codebehind:
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
private void ButtonX_Click(object sender, System.EventArgs e)
{
//send additional parameter value A, B, or C, depending on the button
......
//call next function...
}
What code adjustments should I made?
TIA,
DoughBoy
ASKER
Hi purpleblob,
Thanks for your suggestion, but I am hoping to avoid creating even multiple onclick events.
Regards,
DoughBoy
Thanks for your suggestion, but I am hoping to avoid creating even multiple onclick events.
Regards,
DoughBoy
This requires only a single onclick method but handles the different functionality within the single onclick event thus
private void OnClick(object sender, System.EventArgs e)
{
if(sender == button1)
{
}
else if(sender == button2)
...
}
Therefore when you assignthe click event to a button you simply select this OnClick methid - thus all onclick's get routed to this single method.
private void OnClick(object sender, System.EventArgs e)
{
if(sender == button1)
{
}
else if(sender == button2)
...
}
Therefore when you assignthe click event to a button you simply select this OnClick methid - thus all onclick's get routed to this single method.
ASKER
ok, thanks. then what would the onclick call look like in the aspx page? <asp:button onclick=??? id=somebutton additionalparamter???=???>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Now within the event you can simply test the sender to see which button has been pressed and branch to the relevent code.
i.e.
if(sender == button1)
{
}
else if(sender == button2)
...
Hope this helps