Consuming usercontrol events in a webform

Discussion in 'ASP .Net' started by Craig Douthitt via DotNetMonster.com, Feb 2, 2005.

  1. 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
     
    Craig Douthitt via DotNetMonster.com, Feb 2, 2005
    #1
    1. Advertising

  2. 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" <> wrote in
    message news:...
    > 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
     
    Gopal \(FMS, Inc.\), Feb 2, 2005
    #2
    1. Advertising

  3. 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
     
    Craig Douthitt via DotNetMonster.com, Feb 2, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. titof
    Replies:
    0
    Views:
    517
    titof
    Jul 24, 2003
  2. timmso
    Replies:
    1
    Views:
    503
    Alex Papadimoulis
    Dec 12, 2003
  3. =?Utf-8?B?Sm9l?=

    Wiring events from UserControl in WebForm

    =?Utf-8?B?Sm9l?=, Mar 31, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    362
    =?Utf-8?B?Sm9l?=
    Mar 31, 2006
  4. Trebek
    Replies:
    0
    Views:
    120
    Trebek
    Jul 3, 2003
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    375
    Chris
    Aug 30, 2005
Loading...

Share This Page