UserControl events

CalA

New Member
Hi,<BR><BR>I was wondering if it is possible to add an event to a UserControl. Specifically, if there is a DropDownList in a UserControl, can the main aspx page holding this UserControl respond to the OnSelectedIndexChanged event of the DropDownList?<BR><BR>Are there any articles about this? I have read about accessing the properties and methods of UserControls, but seen nothing about UserControl events.<BR><BR>Thanks,<BR><BR>JON<BR>Within the Page_Load of the UserControl add the following line where "ctlName" is the name of your UserControl:<BR><BR>AddHandler ctlName.SelectedIndexChanged, New System.EventHandler(AddressOf Control_ChangeEvent)<BR><BR>Then, add a method to handle the event in the UserControl:<BR><BR>Public Sub Control_ChangeEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctlName.SelectedIndexChanged<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RaiseEvent OnSelectedIndexChange(Me, e)<BR>End Sub<BR><BR>In your ASPX file, register the UserControl as normal, and add the event handler:<BR><BR>OnSelectedIndexChanged="SomeMethod"<BR><BR>Then in your code-behind, just add a method to handle the event:<BR><BR>Sub SomeMethod(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctlName.OnSelectedIndexChange<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' --- Some Code Here<BR>End Sub<BR><BR><BR>That's it! Hope this helps...<BR><BR><BR>Jason<BR>Hi Jason,<BR><BR>I tried out your code, but got the following error:<BR><BR>"Handles clause requires a WithEvents variable"<BR><BR>pointing to this line as the culprit:<BR><BR>"Public Sub Control_ChangeEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddDropDown.SelectedIndexChanged"<BR><BR><BR>Also, just wanted to check I'd understood you correctly. Where you say "...where "ctlName" is the name of your UserControl...", I assume you mean the id of the DropDownList within the UserControl - correct?<BR><BR>Thanks for your help so far,<BR><BR>JON<BR>Jon,<BR><BR>Yep, "ctlName" should be the id of the DropDownList within the UserControl.<BR><BR>In the code-behind, when you declare your WebControl that inherits from the UserControl, you should declare it as follows:<BR><BR>Protected WithEvents ddDropDown As ClassName<BR><BR>Basically the "WithEvents" just allows the WebControl to handle events such as SelectedIndexChanged. Also make sure that you declare it as the ClassName contained in your UserControl code.<BR><BR><BR>Good luck!<BR><BR>JasonHi Jason,<BR><BR>Aaaargh! Getting confused! This is new stuff for me, and I'm not really getting very far... The latest error message is:<BR><BR> 'OnSelectedIndexChange' is not an event of 'ASP.Maz'<BR><BR>Pointing to the line in the .ascx file:<BR><BR> RaiseEvent OnSelectedIndexChange(Me, e) <BR><BR><BR>Here's the code I'm working on - any chance you can just lead me through the lines I have to change?<BR><BR>Thanks for the help!<BR><BR>JON<BR><BR><BR><BR>++++++++++++++++++++++++++++++++++++++++<BR>JasonMurphy_Code.aspx - the parent page<BR>++++++++++++++++++++++++++++++++++++++++<BR><BR><BR><%@ Page Language="VB" Trace="false" Debug="true" %><BR><%@ Register TagPrefix="UserControl" TagName="ExampleUserControl" src=http://aspmessageboard.com/archive/index.php/"JasonMurphy_Code.ascx" %><BR><BR><BR><script language="VB" runat="server"><BR> <BR>Sub Page_Load(sender as object, e as eventargs)<BR> 'sth here<BR>End Sub<BR><BR>Sub ExampleMethod(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserControl1.OnSelectedIndexChange <BR> ' --- Some Code Here <BR>End Sub <BR><BR></script> <BR><BR><html><BR><body><BR><form runat="server" name="form1" id="form1"><BR> <BR><UserControl:ExampleUserControl id="UserControl1" runat="server" OnSelectedIndexChanged="ExampleMethod" /><BR> <BR></form> <BR></body><BR></html><BR><BR><BR><BR><BR>++++++++++++++++++++++++++++++++++++++++++++++<BR>JasonMurphy_Code.ascx - the User Control page<BR>++++++++++++++++++++++++++++++++++++++++++++++<BR><BR><%@ Control Language="VB" ClassName="Maz" %><BR><BR><script language="VB" runat="server"><BR><BR>Sub Page_Load(obj as object, e as EventArgs)<BR> <BR> Dim myAL1 As New ArrayList()<BR> myAL1.Add("DDL1FirstItem")<BR> myAL1.Add("DDL1SecondItem")<BR> myAL1.Add("DDL1ThirdItem")<BR> DDL1.DataSource = myAL1<BR> DDL1.DataBind()<BR> <BR> Dim myAL2 As New ArrayList()<BR> myAL2.Add("DDL2FirstItem")<BR> myAL2.Add("DDL2SecondItem")<BR> myAL2.Add("DDL2ThirdItem")<BR> DDL2.DataSource = myAL2<BR> DDL2.DataBind()<BR> <BR> AddHandler DDL1.SelectedIndexChanged, New System.EventHandler(AddressOf Control_ChangeEvent1) <BR> AddHandler DDL2.SelectedIndexChanged, New System.EventHandler(AddressOf Control_ChangeEvent2) <BR> <BR>end sub<BR><BR>Protected WithEvents DDL1 As DropDownList <BR>Public Sub Control_ChangeEvent1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DDL1.SelectedIndexChanged <BR> RaiseEvent OnSelectedIndexChange(Me, e) <BR>End Sub <BR><BR>Protected WithEvents DDL2 As DropDownList <BR>Public Sub Control_ChangeEvent2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DDL2.SelectedIndexChanged <BR> RaiseEvent OnSelectedIndexChange(Me, e) <BR>End Sub <BR><BR> <BR></script><BR><html><BR><body><BR><form runat="server"><BR><BR>First DropDown:<BR><asp:DropDownList id="DDL1" runat="server" AutoPostBack="true" /><BR><BR><BR><hr><BR><BR><BR>Second Drop Down:<BR><asp:DropDownList id="DDL2" runat="server" AutoPostBack="true" /><BR><BR></form><BR></body><BR></html>Oops! There's a VERY important line that I forgot to include in the code. Within the code for your UserControl add:<BR><BR>Public Event OnSelectedIndexChange(ByVal Sender As System.Object, ByVal e As System.EventArgs)<BR><BR>That should be the only remaining line. Hopefully I didn't overlook any additional ones, but I don't think I did!<BR><BR><BR>JasonHi James,<BR><BR>Hope you're still with me! Progress is being made, one step at a time....<BR><BR><BR>Have added the line:<BR><BR>Public Event OnSelectedIndexChange(ByVal Sender As System.Object, ByVal e As System.EventArgs)<BR><BR><BR>to the ascx file as suggested, am now getting error:<BR><BR>'DDL1' is already declared as 'Protected Dim WithEvents DDL1 As System.Web.UI.WebControls.DropDownList' in this class.<BR><BR><BR>pointing to this line as culprit:<BR><BR><asp:DropDownList id="DDL1" runat="server" AutoPostBack="true" /><BR><BR><BR>Full code for ascx file below - grateful, as ever, for your help!<BR><BR>Cheers,<BR><BR>JON<BR><BR>*************************************************<BR><BR><%@ Control Language="VB" ClassName="Maz" %><BR><BR><script language="VB" runat="server"><BR><BR>Sub Page_Load(obj as object, e as EventArgs)<BR> <BR> Dim myAL1 As New ArrayList()<BR> myAL1.Add("DDL1FirstItem")<BR> myAL1.Add("DDL1SecondItem")<BR> myAL1.Add("DDL1ThirdItem")<BR> DDL1.DataSource = myAL1<BR> DDL1.DataBind()<BR> <BR> Dim myAL2 As New ArrayList()<BR> myAL2.Add("DDL2FirstItem")<BR> myAL2.Add("DDL2SecondItem")<BR> myAL2.Add("DDL2ThirdItem")<BR> DDL2.DataSource = myAL2<BR> DDL2.DataBind()<BR> <BR> AddHandler DDL1.SelectedIndexChanged, New System.EventHandler(AddressOf Control_ChangeEvent1) <BR> AddHandler DDL2.SelectedIndexChanged, New System.EventHandler(AddressOf Control_ChangeEvent2) <BR> <BR>end sub<BR><BR>Public Event OnSelectedIndexChange(ByVal Sender As System.Object, ByVal e As System.EventArgs) <BR><BR>Protected WithEvents DDL1 As DropDownList <BR>Public Sub Control_ChangeEvent1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DDL1.SelectedIndexChanged <BR> RaiseEvent OnSelectedIndexChange(Me, e) <BR>End Sub <BR><BR>Protected WithEvents DDL2 As DropDownList <BR>Public Sub Control_ChangeEvent2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DDL2.SelectedIndexChanged <BR> RaiseEvent OnSelectedIndexChange(Me, e) <BR>End Sub <BR><BR> <BR></script><BR><html><BR><body><BR><form runat="server"><BR><BR>First DropDown:<BR><asp:DropDownList id="DDL1" runat="server" AutoPostBack="true" /><BR><BR><hr><BR><BR>Second Drop Down:<BR><asp:DropDownList id="DDL2" runat="server" AutoPostBack="true" /><BR><BR></form><BR></body><BR></html><BR><BR>
 
Back
Top