moe
moe

Reputation: 5249

how to call button click event handler from code behind

I have button click event that does an update on my table and this function is called when button is clicked, however i would like to call the same function from code behind and i am not sure if this is even possible. Here is the event handler that i would like to call from code behind:

protected void Update(object sender, GridViewPageEventArgs e)
{
    //do some updates here
}

i have even tried this but did not work, please help

update(null,null);

Upvotes: 1

Views: 8459

Answers (5)

rajeev
rajeev

Reputation: 11

Try

protected void txtModalPosition_TextChanged(object sender, EventArgs e)
{
        Update(sender,e);

}

protected void Update(object sender, GridViewPageEventArgs e)
{
    //do some updates here
}

Or from Page Load you can also call by passing argument

protected void Page_Load(object sender, EventArgs e)
{
  Update(sender,e);           
}

If needed you may also type cast EventArgs to other Classes which inherits it.

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34846

Do not make a "fake" event by calling Update(null, null). This is bad for several reasons:

  1. If you ever decide to use the parameters of the event (object sender or GridViewPageEventArgs e), then your "fake" call passing null values will most likely blow up.
  2. The "click event" paradigm in ASP.NET is actually an implementation of the Observer design pattern, which basically says I want to have a single piece of logic that runs when any dependent object's state changes. In other words, you can have five buttons on a page that all have the same click event logic. Using your Update(null, null) code would break the intent of the Observer pattern.
  3. Putting implementation logic into the click event handler code is almost always a bad idea, as it makes the code less reusable, as you have to pass "fake" values for certain scenarios. Generally speaking your click event handler code should call out to one or many methods that perform various work.
  4. Not separating the implementation logic out into separate methods makes the logic much less testable, as the logic now is dependent upon more than it should (GridView in your case).

I suggest that you do the following:

protected void DoSomeUpdateHere()
{
    // Do your table logic here

}

Now you can call this DoSomeUpdateHere() method from anywhere within your code-behind class (i.e. Page_Load event, other methods, etc.).

Note: Since it is protected it is still accessible by ASP.NET controls in the markup, but not outside of the page itself.

Upvotes: 0

Igor
Igor

Reputation: 15893

You have wrong case of "u" in your "update(null, null);" call. Your approach is fine as long as your don't use sender and e inside Update and you call Update from a method of the same page class.

Upvotes: 1

Johnny
Johnny

Reputation: 491

You can simply call the event handler:

Update(this, new GridViewPageEventArgs());

Just be aware that you can invoke this method, it doesn't mean anything because both the parameters mean nothing to the event handler.

However, you may try to mimic the button's behavior by, say, using

GridViewPageEventArgs e = new GridViewPageEventArgs();
e.NewPageIndex = 2;
Update(GridView1, e);

Upvotes: 0

meda
meda

Reputation: 45490

What about this....

Create a function

doSomeUpdateHere()
{
    //do some updates here
}

Use it in your Button event

protected void Update(object sender, GridViewPageEventArgs e)
{
    doSomeUpdateHere();
}

use it again

doSomeUpdateHere();

Upvotes: 3

Related Questions