[英]Reference static method for ASP.NET/C# event handler
I have a button an my ASP.NET page which currently looks like this:我的 ASP.NET 页面上有一个按钮,目前看起来像这样:
<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;但是,在这种情况下,我要引用的方法是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:也就是说,我只能从
Namespace.Class.Method
而不是ClassInstance.Method
调用它,当我尝试以下操作时,出现错误:
<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.
我更愿意使用一些 ASP.NET 技术而不是 C# 技术,因为我更熟悉 ASP.NET。
you can run it this way;你可以这样运行它;
asp.net code, asp.net代码,
<asp:Button ID="Button1" Text="Click me" OnClick="btn1_Click" />
c# code, c#代码,
private void btn1_Click(object sender, EventArgs e)
{
Namespace.Class.Method();
}
Event handlers can also be added, removed and edited in the code behind.也可以在后台代码中添加、删除和编辑事件处理程序。 For example:
例如:
asp: 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请注意,方法应将 (object sender, EventArgs e) 作为其 arguments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.