mabu mabu - 29 days ago 16
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
System.EventArgs
to
System.Web.UI.WebControls.CommandEventArgs
.

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

Thanks!

Answer

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.

Comments