Attaching event to a control

人走茶凉 提交于 2019-12-08 02:14:56

问题


How to insert an event to the aspx.cs page. I have one asp:button and i wish to add an event of that button in the aspx.cs page. how it done


回答1:


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" />



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1102479/attaching-event-to-a-control

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