?
Solved

Many controls of same type using one common event in codebehind

Posted on 2003-11-06
5
Medium Priority
?
198 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:DoughBoy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:purpleblob
ID: 9697604
Not sure if this is what you want - if what you want is to have all buttons go to the same click event then create a Click event for one button then for each other button from the drop down (when you single click on the Click event for each button) simply choose the Click event you've already created.

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
0
 

Author Comment

by:DoughBoy
ID: 9701344
Hi purpleblob,

  Thanks for your suggestion, but I am hoping to avoid creating even multiple onclick events.

Regards,
DoughBoy
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9701880
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.
0
 

Author Comment

by:DoughBoy
ID: 9702283
ok, thanks. then what would the onclick call look like in the aspx page? <asp:button onclick=??? id=somebutton additionalparamter???=???>
0
 
LVL 6

Accepted Solution

by:
purpleblob earned 200 total points
ID: 9702320
Well the onlick would be in the asp:button it'd be in code behind and thus bound in the InitializeComponent with regards the additonal parameter - surely you can simply do something like

private void OnClick(object sender, System.EventArgs e)
{
   string addtional = null;
   if(sender == button1)
   {
      additional = "do something"
   }
   else if(sender == button2)
   ...
}

Or you don't need the additional parameter because you know which button was pressed via the sender parameter so you should be able to make code decision due to this.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question