1

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
Curtis
  • 101,612
  • 66
  • 270
  • 352

2 Answers2

4

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
Bill
  • 4,425
  • 3
  • 21
  • 22
  • Had to make a slight change, but apart from that, worked perfectly! Sub btnButton_Click(sender as object, e as EventArgs) Handles btnButton.Click RaiseEvent Click(Me, e) End Sub – Curtis Jul 20 '10 at 09:45
0

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
Tisho
  • 8,320
  • 6
  • 44
  • 52
Yogi
  • 410
  • 4
  • 16
  • This should be a comment, not an answer. Check this [metaSO question](http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question) and [Jon Skeet: Coding Blog](http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx) on how to give a correct answer. – Yaroslav Oct 11 '12 at 12:09