Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Consuming usercontrol events in a webform

Reply
Thread Tools

Consuming usercontrol events in a webform

 
 
Craig Douthitt via DotNetMonster.com
Guest
Posts: n/a
 
      02-02-2005
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.

--
Message posted via http://www.dotnetmonster.com
 
Reply With Quote
 
 
 
 
Gopal \(FMS, Inc.\)
Guest
Posts: n/a
 
      02-02-2005
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" <(E-Mail Removed)> wrote in
message news:dd6e7a14dd3044dca92c93e19e2ef772@DotNetMonste r.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.
>
> --
> Message posted via http://www.dotnetmonster.com



 
Reply With Quote
 
 
 
 
Craig Douthitt via DotNetMonster.com
Guest
Posts: n/a
 
      02-02-2005
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)

--
Message posted via http://www.dotnetmonster.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiring events from UserControl in WebForm =?Utf-8?B?Sm9l?= ASP .Net 0 03-31-2006 02:48 PM
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Including WebForm Image Control in a Webform Table Control titof ASP .Net 0 07-24-2003 01:01 PM
WebForm UserControl in IE taking a long time with async events Trebek ASP .Net Web Controls 0 07-03-2003 01:10 PM



Advertisments