I have a button an my ASP.NET page which currently looks like this:
<asp:Button ID="Button1" Text="Click me" />
I want to attach an event handler to the button. Normally, that would look like this:
<asp:Button ID="Button1" Text="Click me" OnClick="SomeFunction" />
However, in this case, the method I want to reference is static; that is, I can only call it from Namespace.Class.Method rather than ClassInstance.Method, When I try the following, I get an error:
<asp:Button ID="Button1" Text="Click me" OnClick="Namespace.Class.Method" />
So, how can I use this method as an event handler? Unfortunately, I do not have control over the namespace. I would prefer to use some ASP.NET technique rather than a C# technique, because I am more familiar with ASP.NET.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Event handlers can also be added, removed and edited in the code behind. For example:
asp:
<asp:Button ID="Button1" Text="Click me" />
code behind:
Button1.Click += new EventHandler(Namespace.Class.Method);
note that Method should take (object sender, EventArgs e) as its arguments
Method 2
you can run it this way;
asp.net code,
<asp:Button ID="Button1" Text="Click me" OnClick="btn1_Click" />
c# code,
private void btn1_Click(object sender, EventArgs e)
{
Namespace.Class.Method();
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0