简体   繁体   English

C#ASP将自定义行为添加到每个System.Web.UI.WebControls.Button.OnClick事件

[英]C# ASP Add custom behavior to every System.Web.UI.WebControls.Button.OnClick event

I want to run some code every time an instance of System.Web.UI.WebControls.Button is clicked. 我想每次单击System.Web.UI.WebControls.Button的实例时运行一些代码。

Is there a way I can drop this functionality into the default OnClick method for the button class, or will I have to make a new class that inherits from System.Web.UI.WebControls.Button? 有没有办法可以将此功能放到按钮类的默认OnClick方法中,还是必须制作一个继承自System.Web.UI.WebControls.Button的新类?

For example everytime I click a button, I want the code 例如,每次我单击按钮时,我都需要代码

Debug.WriteLine ("hello world")
// other code here

to run, but do not want to declare all that code in the OnClick method of every button on my page 运行,但不想在我页面上每个按钮的OnClick方法中声明所有代码

protected void Button1_Click(object sender, EventArgs e)
{
    //when I get here, I want the code above to have run without having to paste it all into this method
}

protected void Button2_Click(object sender, EventArgs e)
{
    //Another button, don't want to be duplicating all that code again
}

Thanks to Mike Cheel who confirmed that I do indeed need to make a new class to achieve this 感谢Mike Cheel,他确认我确实确实需要参加新的课程才能实现这一目标

public class MyButton : System.Web.UI.WebControls.Button
{
    protected override void OnClick(EventArgs e)
    {
        Debug.WriteLine("hello");
    }
}

which is then instantiated in the designer file 然后在设计器文件中实例化

protected global::WebApplication7.MyButton Button1;

and in the cs file for the page I can add my button specific behaviour 在页面的cs文件中,我可以添加按钮的特定行为

protected void Button1_Click(object sender, EventArgs e)
{
    Debug.WriteLine("Goodbye");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Web.UI.WebControls.Button单击事件未触发 - System.Web.UI.WebControls.Button click event not firing ASP.NET C#在标签中显示数据-System.Web.UI.WebControls.Label - asp.net c# display data in label - System.Web.UI.WebControls.Label 在 C# 中将 System.Web.UI.WebControls.Unit 转换为 int - Convert System.Web.UI.WebControls.Unit to int in c# C#—无法将类型&#39;System.Web.UI.Control&#39;隐式转换为&#39;System.Web.UI.WebControls.CheckBox&#39; - C# — Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.CheckBox' C#本机类型的字节等效于System.Web.UI.WebControls.Unit - c# native type for bytes equivalent to System.Web.UI.WebControls.Unit C#:“System.Web.UI.WebControls.Parameter”的未知构造函数“Parameter(String, TypeCode, Int)” - C#: Unknown constructor 'Parameter(String, TypeCode, Int)' of 'System.Web.UI.WebControls.Parameter' 加 <thead> 使用System.Web.UI.WebControls将标签添加到表 - Add <thead> tags to Table using System.Web.UI.WebControls 无法将类型为“ System.Web.UI.WebControls.Button”的对象转换为类型为“ System.Web.UI.WebControls.CheckBox”的对象 - Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.CheckBox' CompilerParameters.ReferencedAssemblies - 添加对System.Web.UI.WebControls的引用 - CompilerParameters.ReferencedAssemblies — Add reference to System.Web.UI.WebControls 无法将“ASP.site_master”类型的对象强制转换为“System.Web.UI.WebControls.Button”。 在asp.net中 - Unable to cast object of type 'ASP.site_master' to type 'System.Web.UI.WebControls.Button'. in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM