problem with selectedvalue of dropdownlist

Discussion in 'ASP .Net' started by Chris, May 17, 2008.

  1. Chris

    Chris Guest

    Hi,

    When choosing a value in the dropdownlist, i try to put that selectedvalue
    in the label, but i always get the first value 'a'.
    EnableViewState is by default set to true, so ... what's wrong in my code?
    Thanks
    Chris

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim ddl As New DropDownList
    Dim z As ListItem
    Dim kz As String
    ddl.AutoPostBack = True
    form1.Controls.Add(ddl)

    If Page.IsPostBack Then
    kz = ddl.SelectedValue
    Label1.Text = kz
    Else
    z = New ListItem("a", "a")
    ddl.Items.Add(z)
    z = New ListItem("b", "b")
    ddl.Items.Add(z)
    z = New ListItem("c", "c")
    ddl.Items.Add(z)
    End If
    End Sub


    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" ></asp:Label>
    </form>
     
    Chris, May 17, 2008
    #1
    1. Advertising

  2. Chris

    Rain Guest

    Put your label setting code in the pre-render event or the index select
    changed event for the dropdown box.




    "Chris" <> wrote in message
    news:...
    > Hi,
    >
    > When choosing a value in the dropdownlist, i try to put that selectedvalue
    > in the label, but i always get the first value 'a'.
    > EnableViewState is by default set to true, so ... what's wrong in my code?
    > Thanks
    > Chris
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim ddl As New DropDownList
    > Dim z As ListItem
    > Dim kz As String
    > ddl.AutoPostBack = True
    > form1.Controls.Add(ddl)
    >
    > If Page.IsPostBack Then
    > kz = ddl.SelectedValue
    > Label1.Text = kz
    > Else
    > z = New ListItem("a", "a")
    > ddl.Items.Add(z)
    > z = New ListItem("b", "b")
    > ddl.Items.Add(z)
    > z = New ListItem("c", "c")
    > ddl.Items.Add(z)
    > End If
    > End Sub
    >
    >
    > <form id="form1" runat="server">
    > <asp:Label ID="Label1" runat="server" ></asp:Label>
    > </form>
    >
     
    Rain, May 17, 2008
    #2
    1. Advertising

  3. Chris

    Lloyd Sheen Guest

    "Chris" <> wrote in message
    news:...
    > Hi,
    >
    > When choosing a value in the dropdownlist, i try to put that selectedvalue
    > in the label, but i always get the first value 'a'.
    > EnableViewState is by default set to true, so ... what's wrong in my code?
    > Thanks
    > Chris
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim ddl As New DropDownList
    > Dim z As ListItem
    > Dim kz As String
    > ddl.AutoPostBack = True
    > form1.Controls.Add(ddl)
    >
    > If Page.IsPostBack Then
    > kz = ddl.SelectedValue
    > Label1.Text = kz
    > Else
    > z = New ListItem("a", "a")
    > ddl.Items.Add(z)
    > z = New ListItem("b", "b")
    > ddl.Items.Add(z)
    > z = New ListItem("c", "c")
    > ddl.Items.Add(z)
    > End If
    > End Sub
    >
    >
    > <form id="form1" runat="server">
    > <asp:Label ID="Label1" runat="server" ></asp:Label>
    > </form>
    >


    You are recreating the dropdownlist every time you select an item. Since
    you have AutoPostBack on you should be using a SelectionChanged routine to
    pull the information from. Why are you creating the dropdown in code?

    LS
     
    Lloyd Sheen, May 17, 2008
    #3
  4. Chris

    Chris Guest

    Thanks for replying.
    I don't know in advance how many dropdownlists i need. This is coming from a
    database, so i have to create them by code.
    Doing like this, there is no SelectionChanged procedure available ...


    "Lloyd Sheen" <> schreef in bericht
    news:...
    >
    > "Chris" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> When choosing a value in the dropdownlist, i try to put that
    >> selectedvalue in the label, but i always get the first value 'a'.
    >> EnableViewState is by default set to true, so ... what's wrong in my
    >> code?
    >> Thanks
    >> Chris
    >>
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim ddl As New DropDownList
    >> Dim z As ListItem
    >> Dim kz As String
    >> ddl.AutoPostBack = True
    >> form1.Controls.Add(ddl)
    >>
    >> If Page.IsPostBack Then
    >> kz = ddl.SelectedValue
    >> Label1.Text = kz
    >> Else
    >> z = New ListItem("a", "a")
    >> ddl.Items.Add(z)
    >> z = New ListItem("b", "b")
    >> ddl.Items.Add(z)
    >> z = New ListItem("c", "c")
    >> ddl.Items.Add(z)
    >> End If
    >> End Sub
    >>
    >>
    >> <form id="form1" runat="server">
    >> <asp:Label ID="Label1" runat="server" ></asp:Label>
    >> </form>
    >>

    >
    > You are recreating the dropdownlist every time you select an item. Since
    > you have AutoPostBack on you should be using a SelectionChanged routine to
    > pull the information from. Why are you creating the dropdown in code?
    >
    > LS
     
    Chris, May 17, 2008
    #4
  5. Chris

    Rain Guest

    Yes there is

    You can create an event handler to service the event. Have a look in the
    help for adding event handlers. Its a fairly simple one to do. This is the
    C# equiv, ( For VB lookup the Addhandler in help ).

    this.DropDownList1.SelectedIndexChanged += new
    System.EventHandler(myFunctionName);

    public void myFunctionName( object sender, System.EventArgs e )








    "Chris" <> wrote in message
    news:...
    > Thanks for replying.
    > I don't know in advance how many dropdownlists i need. This is coming from
    > a database, so i have to create them by code.
    > Doing like this, there is no SelectionChanged procedure available ...
    >
    >
    > "Lloyd Sheen" <> schreef in bericht
    > news:...
    >>
    >> "Chris" <> wrote in message
    >> news:...
    >>> Hi,
    >>>
    >>> When choosing a value in the dropdownlist, i try to put that
    >>> selectedvalue in the label, but i always get the first value 'a'.
    >>> EnableViewState is by default set to true, so ... what's wrong in my
    >>> code?
    >>> Thanks
    >>> Chris
    >>>
    >>> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >>> System.EventArgs) Handles Me.Load
    >>> Dim ddl As New DropDownList
    >>> Dim z As ListItem
    >>> Dim kz As String
    >>> ddl.AutoPostBack = True
    >>> form1.Controls.Add(ddl)
    >>>
    >>> If Page.IsPostBack Then
    >>> kz = ddl.SelectedValue
    >>> Label1.Text = kz
    >>> Else
    >>> z = New ListItem("a", "a")
    >>> ddl.Items.Add(z)
    >>> z = New ListItem("b", "b")
    >>> ddl.Items.Add(z)
    >>> z = New ListItem("c", "c")
    >>> ddl.Items.Add(z)
    >>> End If
    >>> End Sub
    >>>
    >>>
    >>> <form id="form1" runat="server">
    >>> <asp:Label ID="Label1" runat="server" ></asp:Label>
    >>> </form>
    >>>

    >>
    >> You are recreating the dropdownlist every time you select an item. Since
    >> you have AutoPostBack on you should be using a SelectionChanged routine
    >> to pull the information from. Why are you creating the dropdown in code?
    >>
    >> LS

    >
    >
     
    Rain, May 17, 2008
    #5
  6. Chris

    Chris Guest

    I resolved my problem like this:
    Partial Class test
    Inherits System.Web.UI.Page
    Friend ddl As New DropDownList
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load

    Dim z As ListItem
    Dim kz As String
    If Page.IsPostBack Then
    kz = ddl.SelectedValue
    Label1.Text = kz
    Else
    z = New ListItem("a", "a")
    ddl.Items.Add(z)
    z = New ListItem("b", "b")
    ddl.Items.Add(z)
    End If
    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreInit
    ddl.AutoPostBack = True
    form1.Controls.Add(ddl)
    End Sub
    End Class
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreInit
    ddl.AutoPostBack = True
    form1.Controls.Add(ddl)
    End Sub

    But this generates a new problem to me: how can i program that in code?
    Assume i need 5 dropdownlists: i can create dim ddl() and then redim
    ddl(number needed). But how to do that with Friend, which must be outside
    all sub? Or is there a better way to do this?




    "Mark Rae [MVP]" <> schreef in bericht
    news:%...
    > "Rain" <> wrote in message
    > news:%...
    >
    >> You can create an event handler to service the event. Have a look in the
    >> help for adding event handlers. Its a fairly simple one to do. This is
    >> the C# equiv, ( For VB lookup the Addhandler in help ).
    >>
    >> this.DropDownList1.SelectedIndexChanged += new
    >> System.EventHandler(myFunctionName);

    >
    > Remember that dynamic controls which need to have events wired up need to
    > be created in Page_Init or Page_PreInit - Page_Load is usually too late in
    > the page cycle...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Chris, May 17, 2008
    #6
  7. Chris

    Chris Guest

    or maybe better, but still same problem: how to code all the Friend
    variables i need ..

    Partial Class test
    Inherits System.Web.UI.Page
    Friend ddl As New DropDownList

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim z As ListItem
    If Not Page.IsPostBack Then
    z = New ListItem("a", "a")
    ddl.Items.Add(z)
    z = New ListItem("b", "b")
    ddl.Items.Add(z)
    End If
    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreInit
    ddl.AutoPostBack = True
    form1.Controls.Add(ddl)
    AddHandler ddl.SelectedIndexChanged, AddressOf selectddl
    End Sub
    Protected Sub selectddl(ByVal sender As Object, ByVal e As
    System.EventArgs)
    Dim kz As String
    kz = ddl.SelectedValue
    Label1.Text = kz
    End Sub
    End Class





    "Chris" <> schreef in bericht
    news:usM$...
    >I resolved my problem like this:
    > Partial Class test
    > Inherits System.Web.UI.Page
    > Friend ddl As New DropDownList
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    >
    > Dim z As ListItem
    > Dim kz As String
    > If Page.IsPostBack Then
    > kz = ddl.SelectedValue
    > Label1.Text = kz
    > Else
    > z = New ListItem("a", "a")
    > ddl.Items.Add(z)
    > z = New ListItem("b", "b")
    > ddl.Items.Add(z)
    > End If
    > End Sub
    >
    > Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.PreInit
    > ddl.AutoPostBack = True
    > form1.Controls.Add(ddl)
    > End Sub
    > End Class
    > Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.PreInit
    > ddl.AutoPostBack = True
    > form1.Controls.Add(ddl)
    > End Sub
    >
    > But this generates a new problem to me: how can i program that in code?
    > Assume i need 5 dropdownlists: i can create dim ddl() and then redim
    > ddl(number needed). But how to do that with Friend, which must be outside
    > all sub? Or is there a better way to do this?
    >
    >
    >
    >
    > "Mark Rae [MVP]" <> schreef in bericht
    > news:%...
    >> "Rain" <> wrote in message
    >> news:%...
    >>
    >>> You can create an event handler to service the event. Have a look in the
    >>> help for adding event handlers. Its a fairly simple one to do. This is
    >>> the C# equiv, ( For VB lookup the Addhandler in help ).
    >>>
    >>> this.DropDownList1.SelectedIndexChanged += new
    >>> System.EventHandler(myFunctionName);

    >>
    >> Remember that dynamic controls which need to have events wired up need to
    >> be created in Page_Init or Page_PreInit - Page_Load is usually too late
    >> in the page cycle...
    >>
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net

    >
    >
     
    Chris, May 17, 2008
    #7
  8. Chris

    Rain Guest

    Where did I mention doing it in the Page_Load ?


    "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "Rain" <> wrote in message
    > news:%...
    >
    >> You can create an event handler to service the event. Have a look in the
    >> help for adding event handlers. Its a fairly simple one to do. This is
    >> the C# equiv, ( For VB lookup the Addhandler in help ).
    >>
    >> this.DropDownList1.SelectedIndexChanged += new
    >> System.EventHandler(myFunctionName);

    >
    > Remember that dynamic controls which need to have events wired up need to
    > be created in Page_Init or Page_PreInit - Page_Load is usually too late in
    > the page cycle...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Rain, May 17, 2008
    #8
  9. Chris

    Chris Guest

    I don't know ...
    If you really want to help, please just tell me what's wrong
    Thanks


    "Rain" <> schreef in bericht
    news:%...
    > Where did I mention doing it in the Page_Load ?
    >
    >
    > "Mark Rae [MVP]" <> wrote in message
    > news:%...
    >> "Rain" <> wrote in message
    >> news:%...
    >>
    >>> You can create an event handler to service the event. Have a look in the
    >>> help for adding event handlers. Its a fairly simple one to do. This is
    >>> the C# equiv, ( For VB lookup the Addhandler in help ).
    >>>
    >>> this.DropDownList1.SelectedIndexChanged += new
    >>> System.EventHandler(myFunctionName);

    >>
    >> Remember that dynamic controls which need to have events wired up need to
    >> be created in Page_Init or Page_PreInit - Page_Load is usually too late
    >> in the page cycle...
    >>
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net

    >
    >
     
    Chris, May 17, 2008
    #9
  10. Chris

    Rain Guest

    Cool, I didnt think I had

    ;-)

    Cheers


    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Rain" <> wrote in message
    > news:%...
    >
    > [top-posting corrected]
    >
    >>>> You can create an event handler to service the event. Have a look in
    >>>> the help for adding event handlers. Its a fairly simple one to do. This
    >>>> is the C# equiv, ( For VB lookup the Addhandler in help ).
    >>>>
    >>>> this.DropDownList1.SelectedIndexChanged += new
    >>>> System.EventHandler(myFunctionName);
    >>>
    >>> Remember that dynamic controls which need to have events wired up need
    >>> to be created in Page_Init or Page_PreInit - Page_Load is usually too
    >>> late in the page cycle...

    >>
    >> Where did I mention doing it in the Page_Load ?

    >
    > You didn't - that's how it was in the OP's code...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Rain, May 18, 2008
    #10
  11. Chris

    Rain Guest

    This works ( tested ) , hopefully it will help you in formulating your
    solution.

    --------------------
    Private ddls As New ArrayList


    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreInit

    If (Not IsPostBack) Then
    Dim myFirstDDl As New DropDownList
    myFirstDDl.Items.Add(New ListItem("a", "a"))
    myFirstDDl.Items.Add(New ListItem("b", "b"))
    myFirstDDl.Items.Add(New ListItem("c", "c"))
    myFirstDDl.AutoPostBack = True


    'add to ddls array list
    ddls.Add(myFirstDDl)

    'Save between postbacks
    Session("ddls") = ddls
    Else
    'retreive on postback
    ddls = CType(Session("ddls"), ArrayList)

    End If

    For Each d As DropDownList In ddls

    'add to page controls
    form1.Controls.Add(d)
    AddHandler d.SelectedIndexChanged, AddressOf
    handleDDlIndexChanged
    Next


    End Sub

    Protected Sub handleDDlIndexChanged(ByVal sender As Object, ByVal e As
    System.EventArgs)
    Dim ddl As DropDownList = CType(sender, DropDownList)

    Label1.Text = ddl.SelectedValue

    End Sub












    -----------------------------------------------
    "Chris" <> wrote in message
    news:...
    >I don't know ...
    > If you really want to help, please just tell me what's wrong
    > Thanks
    >
    >
    > "Rain" <> schreef in bericht
    > news:%...
    >> Where did I mention doing it in the Page_Load ?
    >>
    >>
    >> "Mark Rae [MVP]" <> wrote in message
    >> news:%...
    >>> "Rain" <> wrote in message
    >>> news:%...
    >>>
    >>>> You can create an event handler to service the event. Have a look in
    >>>> the help for adding event handlers. Its a fairly simple one to do. This
    >>>> is the C# equiv, ( For VB lookup the Addhandler in help ).
    >>>>
    >>>> this.DropDownList1.SelectedIndexChanged += new
    >>>> System.EventHandler(myFunctionName);
    >>>
    >>> Remember that dynamic controls which need to have events wired up need
    >>> to be created in Page_Init or Page_PreInit - Page_Load is usually too
    >>> late in the page cycle...
    >>>
    >>>
    >>> --
    >>> Mark Rae
    >>> ASP.NET MVP
    >>> http://www.markrae.net

    >>
    >>

    >
    >
     
    Rain, May 18, 2008
    #11
  12. Chris

    Chris Guest

    Many thanks.


    "Rain" <> schreef in bericht
    news:%...
    > This works ( tested ) , hopefully it will help you in formulating your
    > solution.
    >
    > --------------------
    > Private ddls As New ArrayList
    >
    >
    > Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.PreInit
    >
    > If (Not IsPostBack) Then
    > Dim myFirstDDl As New DropDownList
    > myFirstDDl.Items.Add(New ListItem("a", "a"))
    > myFirstDDl.Items.Add(New ListItem("b", "b"))
    > myFirstDDl.Items.Add(New ListItem("c", "c"))
    > myFirstDDl.AutoPostBack = True
    >
    >
    > 'add to ddls array list
    > ddls.Add(myFirstDDl)
    >
    > 'Save between postbacks
    > Session("ddls") = ddls
    > Else
    > 'retreive on postback
    > ddls = CType(Session("ddls"), ArrayList)
    >
    > End If
    >
    > For Each d As DropDownList In ddls
    >
    > 'add to page controls
    > form1.Controls.Add(d)
    > AddHandler d.SelectedIndexChanged, AddressOf
    > handleDDlIndexChanged
    > Next
    >
    >
    > End Sub
    >
    > Protected Sub handleDDlIndexChanged(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > Dim ddl As DropDownList = CType(sender, DropDownList)
    >
    > Label1.Text = ddl.SelectedValue
    >
    > End Sub
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > -----------------------------------------------
    > "Chris" <> wrote in message
    > news:...
    >>I don't know ...
    >> If you really want to help, please just tell me what's wrong
    >> Thanks
    >>
    >>
    >> "Rain" <> schreef in bericht
    >> news:%...
    >>> Where did I mention doing it in the Page_Load ?
    >>>
    >>>
    >>> "Mark Rae [MVP]" <> wrote in message
    >>> news:%...
    >>>> "Rain" <> wrote in message
    >>>> news:%...
    >>>>
    >>>>> You can create an event handler to service the event. Have a look in
    >>>>> the help for adding event handlers. Its a fairly simple one to do.
    >>>>> This is the C# equiv, ( For VB lookup the Addhandler in help ).
    >>>>>
    >>>>> this.DropDownList1.SelectedIndexChanged += new
    >>>>> System.EventHandler(myFunctionName);
    >>>>
    >>>> Remember that dynamic controls which need to have events wired up need
    >>>> to be created in Page_Init or Page_PreInit - Page_Load is usually too
    >>>> late in the page cycle...
    >>>>
    >>>>
    >>>> --
    >>>> Mark Rae
    >>>> ASP.NET MVP
    >>>> http://www.markrae.net
    >>>
    >>>

    >>
    >>

    >
    >
     
    Chris, May 18, 2008
    #12
    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. Tom Jorgenson
    Replies:
    3
    Views:
    7,030
  2. Thomas Kern
    Replies:
    1
    Views:
    412
    =?Utf-8?B?YWxhc3Bpbg==?=
    Nov 9, 2004
  3. Ben
    Replies:
    0
    Views:
    3,342
  4. Ben
    Replies:
    0
    Views:
    186
  5. Charleees

    Problem in Setting SelectedValue for DropdownList in Form

    Charleees, Dec 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    123
    Charleees
    Dec 30, 2005
Loading...

Share This Page