Consuming usercontrol events in a webform

  • Thread starter Craig Douthitt via DotNetMonster.com
  • Start date
C

Craig Douthitt via DotNetMonster.com

I am trying to capture an buttonclick on a usercontrol in the webform the usercontrol resides in. After researching this issue, I've come to believe that the best way of handling this is by raising an event in the control and consuming the event in the webform. I tried the following, unfortunatly while the usercontrol raises the event the webform method does not react.

UserControl (Named WebUserControl1 contains one button)

Public Delegate Sub MyEvtHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Event SaveClick As MyEvtHandler

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent SaveClick(sender, e)
End Sub

WebForm

Protected WithEvents ctrl As WebUserControl1

Private Sub handleclick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctrl.SaveClick
Response.Write("Handled")
End Sub


Is there anything else that needs to be set to cause the webform to consume the click event. I am new to ASP so my apologies in advance if the solution is simple.
 
G

Gopal \(FMS, Inc.\)

This has worked for me.
-Control Code Behind
Public Class Upload : Inherits System.Web.UI.UserControl
'...
Public Event UploadDone(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn1.Click
RaiseEvent UploadDone(Me, e)
End Sub
'...
End Class


-Page Code Behind
Public Class ImportEntryFromDelivery : Inherits System.Web.UI.Page
Private WithEvents _Upload1 As Upload
'...
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'...
_Upload1 = CType(Me.FindControl("Upload1"), Upload)
'...
End Sub
Private Sub _Upload1_UploadDone(ByVal sender As Object, ByVal e As
System.EventArgs) Handles _Upload1.UploadDone
'I handle the event
End Sub
'...
End Class

Hope this helps.
--
Gopal Rangaswamy
Microsoft Certified Solutions Developer
FMS, Inc.
<http://www.fmsinc.com/consulting>
<http://www.fmsinc.com/dotnet/SourceBook/>

Craig Douthitt via DotNetMonster.com said:
I am trying to capture an buttonclick on a usercontrol in the webform the
usercontrol resides in. After researching this issue, I've come to believe
that the best way of handling this is by raising an event in the control and
consuming the event in the webform. I tried the following, unfortunatly
while the usercontrol raises the event the webform method does not react.
UserControl (Named WebUserControl1 contains one button)

Public Delegate Sub MyEvtHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Event SaveClick As MyEvtHandler

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseEvent SaveClick(sender, e)
End Sub

WebForm

Protected WithEvents ctrl As WebUserControl1

Private Sub handleclick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ctrl.SaveClick
Response.Write("Handled")
End Sub


Is there anything else that needs to be set to cause the webform to
consume the click event. I am new to ASP so my apologies in advance if the
solution is simple.
 
C

Craig Douthitt via DotNetMonster.com

Thanks for the response. After rewiewing your snippet, the only functional difference was that your code was assigning the actual usercontrol instance in the webform's page_load method so I added the following to my page_load method

ctrl = CType(Me.FindControl("WebUserControl11"), WebUserControl1)

Alas, no luck - the method is still not firing.

(I double-checked to make sure the findcontrol was successful)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top