Wiring events from UserControls

N

news.iq.ca

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
 
N

news.iq.ca

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top