mabu mabu - 1 year ago 105
ASP.NET (C#) Question

Calling click event (CommandEventArgs) of a button serverside

I refer to this article.
I also use ASP.NET and I got the following button click event:

protected void Button1_Click(object sender, CommandEventArgs e)
//Do some stuff

When I try to call the click event serverside I get an error. It says that it can't convert

Is it possible to call the event the same way as in the article I did refer to?


Answer Source

You can absolutely call it still, it is just a normal C# method. But the caveat is that you need to provide correct values as command name and command argument, as this handler most likely uses one or both of these.

string commandName = "command name here";
object commandArgument = <argument here>;
Button1_Click(sender, new CommandEventArgs(commandName, commandArgument));

That is assuming you call it inside Page_Load, as the post you refer to. If not, make sure to provide reference to correct control as sender.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download