Click event handler with Custom Control Button

▼魔方 西西 提交于 2019-12-24 14:30:41

问题


I'm creating an ASP.NET Custom Control for my web application which is basically an ASP.NET Button control contained inside multiple <div> elements (for styling purposes).

How can I create a Click event handler for my custom control so that my control acts as an ASP.NET Button?

Ie.

<cc:Button id="myButton" runat="server" Text="Submit" />

Sub myButton_Click(sender as object, e as EventArgs) Handles myButton.Click

End Sub

回答1:


In the code behind, declare an event in your class:

Public Event Click(sender as object, e as EventArgs)

Then when you handle the click event for the constinuent button, raise the event

Sub btnButton_Click(sender as object, e as EventArgs) Handles btnButton.Click
    RaiseEvent Me.Click(Me, EventArgs)
End Sub



回答2:


Great snippet, came very handy for User Control to raise event where the parent screen needs to be notified. My generic form is like this:

Public Event eventTrigger(ByVal sender As Object, ByVal e As EventArgs)

Private Sub ucUserControl_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtText1.TextChanged, txtText2.TextChanged,txtetc.TextChanged

    RaiseEvent eventTrigger(Me, e)

End Sub


来源:https://stackoverflow.com/questions/3283403/click-event-handler-with-custom-control-button

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