Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Declarative event handling from usercontrol

Reply
Thread Tools

Declarative event handling from usercontrol

 
 
mpaine
Guest
Posts: n/a
 
      09-30-2009
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_SelectedIndexChange d" />
<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" %>

<aspanel ID="TestPanel" runat="server"></aspanel>

<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
 
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
Event not firing in usercontrol inside usercontrol vatech1993@yahoo.com ASP .Net Building Controls 1 12-11-2004 01:28 PM
Event not firing in usercontrol inside usercontrol vatech1993@yahoo.com ASP .Net Building Controls 0 12-10-2004 02:21 PM
Event not firing in usercontrol inside usercontrol vatech1993@yahoo.com ASP .Net Building Controls 0 12-10-2004 02:20 PM
Exception Handling declarative security Patrick ASP .Net Security 1 04-25-2004 12:15 PM
UserControl Declarative Property with Array Andrew Reeves ASP .Net Web Controls 0 10-03-2003 06:37 AM



Advertisments