我一直在 ASP.NET 中创建多个自定义控件 - 主要是为了练习和更深入地了解该语言。
我一直在摆弄一个按钮,但我似乎无法让它处理自己的 OnClick 事件。谁能帮帮我吗?
public class CustomButton : System.Web.UI.WebControls.Button
{
private string href = "";
public CustomButton(string name = "Custom Button", string link = "~/errors/404.aspx", string css = "custom_button")
{
this.Text = name;
this.href = link;
this.CssClass = css;
//this.OnClick += redirect;
this.Attributes.Add("OnClick", "redirect");
}
protected void redirect(object sender, EventArgs e)
{
// I want this function to redirect the user to another page.
}
}
最佳答案
您应该重写 OnClick 方法:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclick.aspx
The OnClick method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
关于c# - 如何让按钮处理 OnClick 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18672240/