views:

894

answers:

7

I have a button in my aspx file called btnTest. The .cs file has a function which is called when the button is clicked.

btnTest_Click(object sender, EventArgs e)

How can I call this function from within my code (i.e. without actually clicking the button)?

+6  A: 
btnTest_Click(null, null);

Provided that the method isn't using either of these parameters (it's very common not to.)

To be honest though this is icky. If you have code that needs to be called you should follow the following convention:

protected void btnTest_Click(object sender, EventArgs e)
{
   SomeSub();
}

protected void SomeOtherFunctionThatNeedsToCallTheCode()
{
   SomeSub();
}

protected void SomeSub()
{
   // ...
}
Spencer Ruport
-1, too much code.
AMissico
No. Not too much code. You shouldn't use a method both for a call and an event. It's misleading and confusing.
Spencer Ruport
Where do you put exception handling? In all three methods? Why create methods just to call a single method? Not confusing. Buttton_Click(null, null) is obvious. SomeOtherFunctionThatNeedsToCallTheCode makes no sense to me.
AMissico
I was using that function name as an example. I could have easily put `foo()` instead.
Spencer Ruport
As far as the exception handling that depends on a lot of factors that extend well beyond the scope of this question.
Spencer Ruport
Doesn't make a difference what you call these other methods. By adding the extra code, you know have to think about it, manage it, and handle any errors this extra code can produce. It is a simple method call, and the name of the method is nearly self-documenting.
AMissico
No it's not self-documented. That's the point. If btnTest_Click is fired at times when btnTest has not been clicked that's misleading. Period.
Spencer Ruport
You know btnTest_Click is not fired because sender is not the button or is null. That is the whole purpose of sender. My point is people write too much code. This is not a button click event. It is just a simple method call.
AMissico
"Period?" It is just a discussion, not an argument.
AMissico
"nearly self-documenting". Adding a comment such as "calling method to avoid creating extra methods" goes a long way.
AMissico
There is no discussion. Having btnTest_Click execute when btnTest has not been clicked is misleading.
Spencer Ruport
System.Windows.Forms.Button.PerformClick()
AMissico
Every language is capable of doing all sorts of nasty things. Doesn't mean you should. :)
Spencer Ruport
A: 

btnTest_Click(new object(), EventArgs.Empty)

Jonathan Allen
passing a generic object seems like a bad idea. better to pass the button or null.
John Knoeller
@John Why do you feel that way?
Jonathan Allen
because the sender is normally the control, so if the click handler uses it at all, it will expect that `sender as Control` will work.
John Knoeller
`(sender as Control)` will return null if you pass in an Object, so that argument doesn't work. My counter argument is that by passing in a null, you break code that uses `(sender.GetType)` to check the type.
Jonathan Allen
A: 

You can call the btnTest_Click just like any other function.

The most basic form would be this:

btnTest_Click(this, null);
NebuSoft
+3  A: 

If the method isn't using either sender or e you could call:

btnTest_Click(null, null);

What you probably should consider doing is extracting the code from within that method into its own method, which you could call from both the button click event handler, and any other places in code that the functionality is required.

Sychare Jedko
+1  A: 

It's just a method on your form, you can call it just like any other method. You just have to create an EventArgs object to pass to it, (and pass it the handle of the button as sender)

John Knoeller
+3  A: 

Use

btnTest_Click( this, new EventArgs() );
JDMX
`EventArgs.Empty`?
Tanzelax
EventArgs.Empty is static of new EventArgs(). So EventArgs.Empty is more appropriate, but creating new is nearly as good.
AMissico
A: 

Simply call

btnTest_Click(null, null);

Just make sure you aren't trying to use either of those params in the function.

Aaron