Attaching event to a control

蹲街弑〆低调 提交于 2019-12-06 07:40:08

Assume that you have a button like that :

<asp:Button runat="server" ID="myButton" />

You can add Click event at your code-behind like that :

myButton.Click += new EventHandler(myButton_Click);

Or you can specify your event at design like that :

<asp:Button runat="server" ID="myButton" OnClick="myButton_Click" />

Just add the following to your .cs file, and amend according to whatever event you are looking to raise.

public delegate void MyEvent(myParams);
public event MyEvent AnEvent;

If it just to handle the click event of your button you can just double click the button and it will automatically take you into the OnClick event in the .cs file.

At compile time, add the text:

OnClick="MethodName"

into the button declaration. MathodName is what gets called when the event is raised (in my example, the Click event).

To dynamically do this, look up the C# or VB.NET syntax for adding and removing event handlers. I think you will also have to ensure ViewState is saved or else the handlers will disappear on the first submit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!