Reputation: 5249
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
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
Reputation: 34846
Do not make a "fake" event by calling Update(null, null)
. This is bad for several reasons:
object sender
or GridViewPageEventArgs e
), then your "fake" call passing null
values will most likely blow up.Update(null, null)
code would break the intent of the Observer pattern.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
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
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
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