getting event to fire from DropDownList user control

Last post 06-29-2007 9:26 AM by GrandVizier. 6 replies.

Sort Posts:

  • getting event to fire from DropDownList user control

    06-28-2007, 9:47 PM

     Hej

    I'm trying to set up a dropdownlist as a user control and everything has been going ok until trying to get the SelectedIndexChanged event to fire - in fact its not even the event that I need to get working but rather having access to the SelectedItem.Value  - I've been working with ASP.Net for a while now but I'm new to user controls (really looking forward to using their potential) - I've followed several tutorials and forum posts but I just can't figure out what I'm missing - I've supplied the majority of the code I'm using  

    'USER CONTROL
    Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList
    Private SelValue As Integer

    Public Event
    ListChanged(ByVal sender As Object, ByVal e As ListArgs)

    Protected Sub ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
    Dim la As New ListArgs
    la.itemValue = ddl.SelectedItem.Value
    SelValue = ddl.SelectedItem.Value
    RaiseEvent ListChanged(sender, la)
    End Sub

    Public Class
    ListArgs
    Inherits EventArgs
    Public itemValue As Integer
    End Class

    Private Sub
    Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then 'Data binding code is here End If
    End Sub

    'as you can see I tried different techniques with this property
    Public Property
    SelectedItemValue() As Integer
    Get
    Return
    SelValue
    End Get
    Set
    (ByVal Value As Integer)
    SelValue = Me.ddl.SelectedValue
    End Set
    End Property
    
    <asp:dropdownlist id="ddl" runat="server" AutoPostBack="True"></asp:dropdownlist>
      

     

    'Web Form
    'These are the methods that I expected to be working but ucDropDownList1_ListChanged never seems to be called
    Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
    Response.Redirect("newPage.aspx?ID=" & ucDropDownList1.SelValue)
    End Sub

    Private Sub
    ucDropDownList1_ListChanged(ByVal sender As System.Object, ByVal e As LaResBeer.ucDropDownList.ListArgs) Handles ucDropDownList1.ListChanged
    Label1.Text = e.itemValue.ToString
    ucDropDownList1.SelValue = e.itemValue
    End Sub
       
    <%@ Page Language="vb" AutoEventWireup="false"%>
    <%@ Register TagPrefix="uc1" TagName="ucDropDownList" src="ucDropDownList.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    .....
    <uc1:ucDropDownList id="UcDropDownList1" runat="server"></uc1:ucDropDownList>
     

     

    hopefully somebody can see what I'm doing wrong and help me fix this - thanks

     

     

  • Re: getting event to fire from DropDownList user control

    06-28-2007, 10:35 PM
    • Loading...
    • zaladane
    • Joined on 06-19-2007, 6:44 AM
    • California
    • Posts 178

    Oups!

     

    Still in search of the far side.
    --------------------------------
    A code snippet may help you, but a reference to the doc will teach you something for later...Don't just try to solve somebody's problem with a snippet, teach him how to solve it.
  • Re: getting event to fire from DropDownList user control

    06-28-2007, 10:38 PM
    Answer
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 7,729
    • Moderator
      TrustedFriends-MVPs

    if you try to store the selected value from you ddl in another variable, you will need to add additional code to get it to survive postbacks.

    how about simply adding a readonly property that reads the selectedvalue directly from your inner dropdownlist

        Public ReadOnly Property SelectedValue() As String
            Get
                Return Me.DropDownList1.SelectedValue
            End Get
        End Property
    
      

     

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: getting event to fire from DropDownList user control

    06-28-2007, 10:48 PM
    • Loading...
    • zaladane
    • Joined on 06-19-2007, 6:44 AM
    • California
    • Posts 178

    interesting, i wouldn't have thought of postbacks...i guess we learn everyday.Smile

    Still in search of the far side.
    --------------------------------
    A code snippet may help you, but a reference to the doc will teach you something for later...Don't just try to solve somebody's problem with a snippet, teach him how to solve it.
  • Re: getting event to fire from DropDownList user control

    06-28-2007, 10:56 PM

    ahh, see that just makes sense 

    unfortunately though, now I'm getting a "Object reference not set to an instance of an object" error when the Get is called

  • Re: getting event to fire from DropDownList user control

    06-28-2007, 11:05 PM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 7,729
    • Moderator
      TrustedFriends-MVPs

    did you change the id to match your specific markup?

     Return Me.ddl.SelectedValue

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: getting event to fire from DropDownList user control

    06-29-2007, 9:26 AM

     yeah, I changed the control's id (I don't think it would have even compiled if I didn't)

     I found my problem now though - for some reason I found the need on my Web Form to add the line

    Me.ucDropDownList1 = New ucDropDownList

     under InitializeComponent() - by removing that everythings working fine

    Thanks
     

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter