【发布时间】:2017-08-09 16:16:38
【问题描述】:
我想在每次单击 System.Web.UI.WebControls.Button 的实例时运行一些代码。
有没有办法将此功能放入按钮类的默认 OnClick 方法中,还是必须创建一个继承自 System.Web.UI.WebControls.Button 的新类?
例如每次我点击一个按钮,我都想要代码
Debug.WriteLine ("hello world")
// other code here
运行, 但不想在我页面上每个按钮的 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
}
【问题讨论】:
-
您是否尝试过创建自己的继承自 Button 的按钮?您也许可以这样做,然后根据需要覆盖。我发现了这个:stackoverflow.com/questions/232934/…