Solved

OnClick Variables

Posted on 2011-03-08
4
393 Views
Last Modified: 2012-05-11
Hello, I am using C# to create an ASP.NET website.  Here's my issue:

I have a standard button on one page.  This button has an onclick setup to fire a method.  I have a second button on this page.  When the user clicks on this button, its onclick setup fires another method which does some stuff, BUT at the end of this method, I would like it to fire the first button's onclick method.

Currently when I add the name of the first button's onclick method ( button1_Click() ) to the end of the second button's onclick method, this error is displayed: "No overload method for..takes 0 arguments".

I know what this error means: the system wants me to include two variables when calling button1_click, which are the default:  object sender, EventArgs e

What are object sender, EventArgs e and how do I include them in my call to button1_Click from the button2_click method?

Thank you in advance.
0
Comment
Question by:karlhsc
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35073996
"Sender" refers to the control that raised the event. "EventArgs" will is an object that contains data about the event, in the case of a button click event eventargs doesn't tell you anything useful. If you want to manually call the method associated with the click event you can safely pass null for both arguments:
button1_Click(null, null);

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35074096
carl_tawn is absolutely correct, I would only expand on his response slightly to say that it is usually safe to pass null for both arguments - it certainly seems to be the case you could safely do so given your current example, just wanted to point out it's not always safe (99% safe, 1% not safe).

Also, I would suggest moving the code that actually does "stuff" into it's own method(s), and then calling those methods from the button event handlers:
private void Button1_Click(object sender, EventArgs e)
{
  // Button 1 just does stuff.
  DoStuff();
}

private void Button2_Click(object sender, EventArgs e)
{
  // Button 2 does stuff, and also does other stuff
  DoStuff();
  DoOtherStuff();
}

private void DoStuff()
{
  // Do some stuff 
}

private void DoOtherStuff()
{
  // Do some other stuff
}

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35074147
@tgerbert - You are of course correct - just figured it wasn't likely to be an issue in this scenario so skimmed over it :)

Just to expand a little. It is possible to use a single event handler as the target for an event on multiple controls. In a scenario like that you may want to cast "sender" in order to find out which control raised the event and act accordingly.
0
 

Author Comment

by:karlhsc
ID: 35074186
tgerbert: thank you for the additional information.  I see the merit in the architecture you provided.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 32
Iteration Help (Asp.net VB) 5 24
C# MVC Insert Multiple Row into DB 2 31
Need help converting bitmap to image in VB.Net 8 29
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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