Wiring events from UserControls

Discussion in 'ASP .Net Web Controls' started by news.iq.ca, Dec 4, 2004.

  1. news.iq.ca

    news.iq.ca Guest

    Hello,

    I have created a UserControl (actually, a From/To control, containing two
    list boxes and four buttons: MoveLeft, MoveAllLeft, MoveRight,
    MoveAllRight). It works fine, but now I need to raise the following two
    events so that in the container I can, depending on whether the "TO" list is
    empty or not, enable or not the "Next Page" button.

    In the control (ListToFrom.ascx), the code looks like this:

    ----------------------------------------------------------------
    Protected WithEvents lstFrom As System.Web.UI.WebControls.ListBox

    Public Event ListFrom_Empty()

    Protected WithEvents lstTo As System.Web.UI.WebControls.ListBox

    Public Event ListTo_Empty()

    Protected WithEvents cmdRight As System.Web.UI.WebControls.Button

    Protected WithEvents cmdRightAll As System.Web.UI.WebControls.Button

    Protected WithEvents cmdLeft As System.Web.UI.WebControls.Button

    Protected WithEvents cmdLeftAll As System.Web.UI.WebControls.Button

    blah, blah, blah, and:

    Private Sub lstFrom_SelectedIndexChanged(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles lstFrom.SelectedIndexChanged

    If lstFrom.Items.Count = 0 Then RaiseEvent ListFrom_Empty()

    End Sub

    Private Sub lstTo_SelectedIndexChanged(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles lstTo.SelectedIndexChanged

    If lstTo.Items.Count = 0 Then RaiseEvent ListTo_Empty()

    End Sub
    ----------------------------------------------------------------

    In the container, I have:

    ----------------------------------------------------------------
    <%@ Register TagPrefix="Raducu" TagName="ListToFrom" src="ListToFrom.ascx"%>
    ----------------------------------------------------------------

    But I can't receive the events raised by the UserControl (I can't define it
    as being "withevents").

    I tried

    Private WithEvents m_objListToFrom As ListToFrom

    and, yes, I can see the events, but this is NOT my Usercontrol, so the real
    event never actually occurs:

    ----------------------------------------------------------------
    Private Sub m_objListToFrom_ToEmpty() Handles m_objListToFrom.ListTo_Empty

    cmdNext.Enabled = True
    End Sub
    ----------------------------------------------------------------

    How can I, then, wire these events ? Should I use AddHandler ? In so, how ?

    Thank you.
    Alex
     
    news.iq.ca, Dec 4, 2004
    #1
    1. Advertising

  2. news.iq.ca

    news.iq.ca Guest

    Sorry, I will make myself clearer (and I'll also correct a mistake in the
    previous post):

    The code in the ascx is:

    <code>
    Private Sub cmdAny_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdLeft.Click, cmdLeftAll.Click, cmdRight.Click,
    cmdRightAll.Click

    Dim Item As ListItem

    Select Case sender.id
    Case "cmdRight"
    If lstFrom.SelectedIndex > -1 Then
    lstTo.SelectedIndex = -1
    lstTo.Items.Add(lstFrom.SelectedItem)
    lstFrom.Items.Remove(lstFrom.SelectedItem)
    lstFrom.SelectedIndex = 0
    End If
    Case "cmdRightAll"
    For Each Item In lstFrom.Items
    lstTo.Items.Add(Item)
    Next Item
    lstFrom.Items.Clear()
    Case "cmdLeft"
    If lstTo.SelectedIndex > -1 Then
    lstFrom.SelectedIndex = -1
    lstFrom.Items.Add(lstTo.SelectedItem)
    lstTo.Items.Remove(lstTo.SelectedItem)
    lstTo.SelectedIndex = 0
    End If
    Case "cmdLeftAll"
    For Each Item In lstTo.Items
    lstFrom.Items.Add(Item)
    Next Item
    lstTo.Items.Clear()
    End Select
    Item = Nothing
    If lstFrom.Items.Count = 0 Then RaiseEvent ListFrom_Empty()
    If lstTo.Items.Count = 0 Then RaiseEvent ListTo_Empty()
    End Sub
    </code>

    In the client, where I try to consume the event, I wrote:
    <code>

    Protected WithEvents m_objListToFrom As ListToFrom

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    If Not Page.IsPostBack Then
    cmdNext.Enabled = False
    cmdPrevious.Enabled = False
    m_objListToFrom = New ListToFrom()
    AddHandler m_objListToFrom.ListTo_Empty, AddressOf Test
    End If
    End Sub

    Private Sub Test()
    cmdNext.Enabled = True
    End Sub
    </code>

    The events are raised correctly but, however, nothing happens in the
    client. What am I doing wrong here ?

    Thank you.
     
    news.iq.ca, Dec 4, 2004
    #2
    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. Oisin Grehan
    Replies:
    3
    Views:
    527
    Natty Gur
    Jul 15, 2003
  2. =?Utf-8?B?RWxkb24gRmVycmFuIGRlIFBvbA==?=

    Wiring up events nightmare

    =?Utf-8?B?RWxkb24gRmVycmFuIGRlIFBvbA==?=, Dec 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    328
    Rajagopal Pasupuleti
    Dec 17, 2004
  3. =?Utf-8?B?RWxkb24gRmVycmFuIGRlIFBvbA==?=

    Wiring Up Events Nightmare

    =?Utf-8?B?RWxkb24gRmVycmFuIGRlIFBvbA==?=, Dec 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    433
    John Saunders
    Dec 17, 2004
  4. =?Utf-8?B?Sm9l?=

    Wiring events from UserControl in WebForm

    =?Utf-8?B?Sm9l?=, Mar 31, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    369
    =?Utf-8?B?Sm9l?=
    Mar 31, 2006
  5. Zamdrist

    Wiring up events...

    Zamdrist, Jan 24, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    115
    Zamdrist
    Jan 24, 2006
Loading...

Share This Page