gpt4 book ai didi

c# - 如何使按钮处理它的 OnClick 事件?

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:10 25 4
gpt4 key购买 nike

我一直在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com