Declarative event handling from usercontrol

Discussion in 'ASP .Net Web Controls' started by mpaine, Sep 30, 2009.

  1. mpaine

    mpaine Guest

    Hello,

    I am trying to figure out how to declaratively pass in a event handler into
    a user control, but I am stumped. All I can make work is the user control's
    event handler.. I can't seem to bubble up the caught event into the parent
    page. Ideas would be quite welcome. Here is my code:


    Default.aspx:

    <%@ Page Language="VB" %>
    <%@ Register TagPrefix="rpt" TagName="filter" Src="WebUserControl.ascx" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Test Controls</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <rpt:filter ID="DataView1Filters" runat="server"
    SelectedIndexChanged="DropDown_SelectedIndexChanged" />
    <asp:Label ID="Label1" runat="server" />
    </form>

    <script runat="server">
    Public Sub DropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e
    As EventArgs)
    Label1.Text = String.Format("Inside declarative event handler.
    {0}<br>", Label1.Text)
    End Sub
    </script>

    </body>
    </html>


    WebUserControl.ascx:

    <%@ Control Language="VB" ClassName="WebUserControlTest" %>

    <asp:panel ID="TestPanel" runat="server"></asp:panel>

    <script runat="server">

    Private AllEvents As New System.ComponentModel.EventHandlerList
    Public Custom Event SelectedIndexChanged As EventHandler
    AddHandler(ByVal value As EventHandler)
    AllEvents.AddHandler("SelectedIndexChanged", value)
    End AddHandler

    RemoveHandler(ByVal value As EventHandler)
    AllEvents.RemoveHandler("SelectedIndexChanged", value)
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim value As EventHandler =
    CType(AllEvents("SelectedIndexChanged"), EventHandler)
    If Not value Is Nothing Then
    value.Invoke(sender, e)
    End If
    End RaiseEvent
    End Event

    Private Sub _SelectedIndexChanged(ByVal sender As Object, ByVal e As
    EventArgs)
    Dim ctrl As DropDownList = Me.FindControl("TestDropDownList")
    If Not ctrl Is Nothing Then
    Me.ViewState("ItemSelection") = ctrl.SelectedIndex
    End If
    Dim Label1 As Label = Parent.FindControl("Label1")
    Label1.Text = String.Format("Inside user control event handler.
    {0}<br>", Label1.Text)
    RaiseEvent SelectedIndexChanged(sender, e)
    End Sub

    Private Overloads Sub OnLoad(ByVal sender As Object, ByVal e As
    EventArgs) Handles Me.Load
    Dim ctrl As New DropDownList
    With ctrl
    .ID = "TestDropDownList"
    .Items.Clear()
    .AutoPostBack = True
    AddHandler .SelectedIndexChanged, AddressOf _SelectedIndexChanged
    .Items.Add(New ListItem("-- Select --", String.Empty))
    .Items.Add(New ListItem("Item 1", "1"))
    .Items.Add(New ListItem("Item 2", "2"))
    If Not Me.ViewState("ItemSelection") Is Nothing Then
    .SelectedIndex = CInt(Me.ViewState("ItemSelection"))
    Else
    .SelectedIndex = 0
    End If
    End With
    TestPanel.Controls.Add(ctrl)
    End Sub

    </script>



    Thanks!


    --
    msdn premium subscriber
     
    mpaine, Sep 30, 2009
    #1
    1. Advertisements

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. huobazi
    Replies:
    1
    Views:
    872
    David Waz...
    Jul 3, 2003
  2. Colin Ramsay

    Dynamic Datagrid UserControl Event Handling

    Colin Ramsay, Apr 17, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    874
    Colin Ramsay
    Apr 17, 2004
  3. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    346
  4. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    318
    vatech1993
    Dec 11, 2004
  5. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    284
  6. Patrick

    Exception Handling declarative security

    Patrick, Apr 23, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    252
    Chris Rolon
    Apr 25, 2004
  7. Andrew Reeves

    UserControl Declarative Property with Array

    Andrew Reeves, Oct 3, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    246
    Andrew Reeves
    Oct 3, 2003
  8. John Hopper

    handling usercontrol event in container page in .net 2.0

    John Hopper, May 30, 2008, in forum: ASP .Net Web Controls
    Replies:
    6
    Views:
    245
    John Hopper
    Jun 2, 2008
Loading...