Get Value of Selected Item From Drop Down List

Discussion in 'ASP .Net' started by Stephen Adam, Nov 30, 2004.

  1. Stephen Adam

    Stephen Adam Guest

    Hi there,

    I'm sure i'm missing something really simple here, all i want to do is get
    the value of the selected item in a list box. Even after much fiddling about
    last night I still could not get my code to work. Below is some code which
    highlights my problem. All I want to do is set the lable control's text
    property to the value of the selected drop down list value - in this example
    i've shown the three ways i've tried.

    Please help!

    Thanks

    Steve


    VB CODE

    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Protected WithEvents ddlNames As System.Web.UI.WebControls.DropDownList
    Protected WithEvents lblResult As System.Web.UI.WebControls.Label
    Protected WithEvents btnRun As System.Web.UI.WebControls.Button

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Private Sub Run(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnRun.Click
    ' None of these work :(
    lblResult.Text = ddlNames.SelectedValue
    lblResult.Text = ddlNames.SelectedValue.ToString
    lblResult.Text = ddlNames.SelectedItem.Text
    End Sub

    End Class

    ASPX CODE

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
    Inherits="dropdownlist.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema
    content="http://schemas.microsoft.com/intellisense/ie5">
    </head>
    <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">
    <h1> Drop Down List Example </h1>
    </br>
    </br>

    <asp:DropDownList ID="ddlName" Runat="server">
    <asp:ListItem Selected="True"
    Value="Bill">Bill</asp:ListItem>
    <asp:ListItem Value="Jason">Jason</asp:ListItem>
    <asp:ListItem Value="Tim">Tim</asp:ListItem>
    </asp:DropDownList>

    </br>
    </br>

    <asp:Button ID="btnRun" Runat="server" Text="Run"></asp:Button>
    </br>
    </br>

    <asp:Label ID="lblResult" Runat="server"> </asp:Label>
    </form>

    </body>
    </html>
    Stephen Adam, Nov 30, 2004
    #1
    1. Advertising

  2. The problem may be that your page is posting back when you press the button
    and thus losing the .selectedvalue of the ddl. Place something like this in
    your page load.

    If Not IsPostBack Then

    'bind the drop-down-list control using code

    Dim Adapter As New SqlDataAdapter("SELECT * FROM tblName",
    "SQLConnectionGoesHERE")
    Dim Dataset As New DataSet
    Adapter.Fill(Dataset, "tblName")

    Me.ddl.DataMember = "tblName"
    Me.ddl.DataSource = Dataset
    Me.ddl.DataTextField = "DataTextField"
    Me.ddl.DataValueField = "DataValueField"
    Me.ddl.DataBind()

    End If

    HTH

    "Stephen Adam" wrote:

    > Hi there,
    >
    > I'm sure i'm missing something really simple here, all i want to do is get
    > the value of the selected item in a list box. Even after much fiddling about
    > last night I still could not get my code to work. Below is some code which
    > highlights my problem. All I want to do is set the lable control's text
    > property to the value of the selected drop down list value - in this example
    > i've shown the three ways i've tried.
    >
    > Please help!
    >
    > Thanks
    >
    > Steve
    >
    >
    > VB CODE
    >
    > Public Class WebForm1
    > Inherits System.Web.UI.Page
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    >
    > 'NOTE: The following placeholder declaration is required by the Web Form
    > Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Protected WithEvents ddlNames As System.Web.UI.WebControls.DropDownList
    > Protected WithEvents lblResult As System.Web.UI.WebControls.Label
    > Protected WithEvents btnRun As System.Web.UI.WebControls.Button
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > Private Sub Run(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles btnRun.Click
    > ' None of these work :(
    > lblResult.Text = ddlNames.SelectedValue
    > lblResult.Text = ddlNames.SelectedValue.ToString
    > lblResult.Text = ddlNames.SelectedItem.Text
    > End Sub
    >
    > End Class
    >
    > ASPX CODE
    >
    > <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
    > Inherits="dropdownlist.WebForm1"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <html>
    > <head>
    > <title>WebForm1</title>
    > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > <meta name=vs_defaultClientScript content="JavaScript">
    > <meta name=vs_targetSchema
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </head>
    > <body MS_POSITIONING="GridLayout">
    >
    > <form id="Form1" method="post" runat="server">
    > <h1> Drop Down List Example </h1>
    > </br>
    > </br>
    >
    > <asp:DropDownList ID="ddlName" Runat="server">
    > <asp:ListItem Selected="True"
    > Value="Bill">Bill</asp:ListItem>
    > <asp:ListItem Value="Jason">Jason</asp:ListItem>
    > <asp:ListItem Value="Tim">Tim</asp:ListItem>
    > </asp:DropDownList>
    >
    > </br>
    > </br>
    >
    > <asp:Button ID="btnRun" Runat="server" Text="Run"></asp:Button>
    > </br>
    > </br>
    >
    > <asp:Label ID="lblResult" Runat="server"> </asp:Label>
    > </form>
    >
    > </body>
    > </html>
    >
    >
    >
    =?Utf-8?B?TXJNaWtl?=, Nov 30, 2004
    #2
    1. Advertising

  3. Stephen Adam

    Stephen Adam Guest

    I've tried following your example code and now have this, which still does
    not populate the lists or let me access them!

    Public Class search

    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init

    'CODEGEN: This method call is required by the Web Form Designer

    'Do not modify it using the code editor.

    InitializeComponent()

    End Sub

    #End Region

    Protected WithEvents ddlRegion As DropDownList

    Protected WithEvents ddlPrice As DropDownList

    Protected WithEvents ddlTheme As DropDownList

    Protected WithEvents lblMessage As Label

    Protected strRegion As String

    Protected strPrice As String

    Protected strTheme As String



    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    If Not (Page.IsPostBack) Then

    Me.ddlRegion = New DropDownList

    Me.ddlRegion.Items.Add("All regions")

    Me.ddlRegion.Items.Add("Devon & Cornwall")

    Me.ddlRegion.Items.Add("East of England")

    Me.ddlRegion.Items.Add("East Midlands")

    Me.ddlRegion.Items.Add("London")

    Me.ddlRegion.Items.Add("North West")

    Me.ddlRegion.Items.Add("South East")

    Me.ddlRegion.Items.Add("Thames & Solent")

    Me.ddlRegion.Items.Add("Wessex")

    Me.ddlRegion.Items.Add("West Midlands")

    Me.ddlRegion.Items.Add("Yorkshire & North East")

    Me.ddlRegion.DataBind()



    Me.ddlPrice = New DropDownList

    Me.ddlPrice.Items.Add("All Prices")

    Me.ddlPrice.Items.Add("Under £50")

    Me.ddlPrice.Items.Add("Over £50 less than £100")

    Me.ddlPrice.Items.Add("More then £100")

    Me.ddlPrice.DataBind()

    Me.ddlTheme = New DropDownList

    Me.ddlTheme.Items.Add("All Themes")

    Me.ddlTheme.Items.Add("City")

    Me.ddlTheme.Items.Add("Country")

    Me.ddlTheme.Items.Add("Seaside")

    Me.ddlTheme.DataBind()





    End If



    End Sub

    Function Price()

    Return strPrice

    End Function

    Function Region()

    Return strRegion

    End Function

    Function Theme()

    Return strTheme

    End Function



    End Class


    "MrMike" <> wrote in message
    news:...
    > The problem may be that your page is posting back when you press the

    button
    > and thus losing the .selectedvalue of the ddl. Place something like this

    in
    > your page load.
    >
    > If Not IsPostBack Then
    >
    > 'bind the drop-down-list control using code
    >
    > Dim Adapter As New SqlDataAdapter("SELECT * FROM tblName",
    > "SQLConnectionGoesHERE")
    > Dim Dataset As New DataSet
    > Adapter.Fill(Dataset, "tblName")
    >
    > Me.ddl.DataMember = "tblName"
    > Me.ddl.DataSource = Dataset
    > Me.ddl.DataTextField = "DataTextField"
    > Me.ddl.DataValueField = "DataValueField"
    > Me.ddl.DataBind()
    >
    > End If
    >
    > HTH
    >
    > "Stephen Adam" wrote:
    >
    > > Hi there,
    > >
    > > I'm sure i'm missing something really simple here, all i want to do is

    get
    > > the value of the selected item in a list box. Even after much fiddling

    about
    > > last night I still could not get my code to work. Below is some code

    which
    > > highlights my problem. All I want to do is set the lable control's text
    > > property to the value of the selected drop down list value - in this

    example
    > > i've shown the three ways i've tried.
    > >
    > > Please help!
    > >
    > > Thanks
    > >
    > > Steve
    > >
    > >
    > > VB CODE
    > >
    > > Public Class WebForm1
    > > Inherits System.Web.UI.Page
    > >
    > > #Region " Web Form Designer Generated Code "
    > >
    > > 'This call is required by the Web Form Designer.
    > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > InitializeComponent()
    > >
    > > End Sub
    > >
    > > 'NOTE: The following placeholder declaration is required by the Web

    Form
    > > Designer.
    > > 'Do not delete or move it.
    > > Private designerPlaceholderDeclaration As System.Object
    > >
    > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Init
    > > 'CODEGEN: This method call is required by the Web Form Designer
    > > 'Do not modify it using the code editor.
    > > InitializeComponent()
    > > End Sub
    > >
    > > #End Region
    > >
    > > Protected WithEvents ddlNames As

    System.Web.UI.WebControls.DropDownList
    > > Protected WithEvents lblResult As System.Web.UI.WebControls.Label
    > > Protected WithEvents btnRun As System.Web.UI.WebControls.Button
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > > 'Put user code to initialize the page here
    > > End Sub
    > >
    > > Private Sub Run(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles btnRun.Click
    > > ' None of these work :(
    > > lblResult.Text = ddlNames.SelectedValue
    > > lblResult.Text = ddlNames.SelectedValue.ToString
    > > lblResult.Text = ddlNames.SelectedItem.Text
    > > End Sub
    > >
    > > End Class
    > >
    > > ASPX CODE
    > >
    > > <%@ Page Language="vb" AutoEventWireup="false"

    Codebehind="WebForm1.aspx.vb"
    > > Inherits="dropdownlist.WebForm1"%>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > > <html>
    > > <head>
    > > <title>WebForm1</title>
    > > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > > <meta name=vs_defaultClientScript content="JavaScript">
    > > <meta name=vs_targetSchema
    > > content="http://schemas.microsoft.com/intellisense/ie5">
    > > </head>
    > > <body MS_POSITIONING="GridLayout">
    > >
    > > <form id="Form1" method="post" runat="server">
    > > <h1> Drop Down List Example </h1>
    > > </br>
    > > </br>
    > >
    > > <asp:DropDownList ID="ddlName" Runat="server">
    > > <asp:ListItem Selected="True"
    > > Value="Bill">Bill</asp:ListItem>
    > > <asp:ListItem Value="Jason">Jason</asp:ListItem>
    > > <asp:ListItem Value="Tim">Tim</asp:ListItem>
    > > </asp:DropDownList>
    > >
    > > </br>
    > > </br>
    > >
    > > <asp:Button ID="btnRun" Runat="server"

    Text="Run"></asp:Button>
    > > </br>
    > > </br>
    > >
    > > <asp:Label ID="lblResult" Runat="server"> </asp:Label>
    > > </form>
    > >
    > > </body>
    > > </html>
    > >
    > >
    > >
    Stephen Adam, Nov 30, 2004
    #3
  4. Stephen Adam

    Stephen Adam Guest

    Twas a spelling mistake!
    "MrMike" <> wrote in message
    news:...
    > The problem may be that your page is posting back when you press the

    button
    > and thus losing the .selectedvalue of the ddl. Place something like this

    in
    > your page load.
    >
    > If Not IsPostBack Then
    >
    > 'bind the drop-down-list control using code
    >
    > Dim Adapter As New SqlDataAdapter("SELECT * FROM tblName",
    > "SQLConnectionGoesHERE")
    > Dim Dataset As New DataSet
    > Adapter.Fill(Dataset, "tblName")
    >
    > Me.ddl.DataMember = "tblName"
    > Me.ddl.DataSource = Dataset
    > Me.ddl.DataTextField = "DataTextField"
    > Me.ddl.DataValueField = "DataValueField"
    > Me.ddl.DataBind()
    >
    > End If
    >
    > HTH
    >
    > "Stephen Adam" wrote:
    >
    > > Hi there,
    > >
    > > I'm sure i'm missing something really simple here, all i want to do is

    get
    > > the value of the selected item in a list box. Even after much fiddling

    about
    > > last night I still could not get my code to work. Below is some code

    which
    > > highlights my problem. All I want to do is set the lable control's text
    > > property to the value of the selected drop down list value - in this

    example
    > > i've shown the three ways i've tried.
    > >
    > > Please help!
    > >
    > > Thanks
    > >
    > > Steve
    > >
    > >
    > > VB CODE
    > >
    > > Public Class WebForm1
    > > Inherits System.Web.UI.Page
    > >
    > > #Region " Web Form Designer Generated Code "
    > >
    > > 'This call is required by the Web Form Designer.
    > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > InitializeComponent()
    > >
    > > End Sub
    > >
    > > 'NOTE: The following placeholder declaration is required by the Web

    Form
    > > Designer.
    > > 'Do not delete or move it.
    > > Private designerPlaceholderDeclaration As System.Object
    > >
    > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Init
    > > 'CODEGEN: This method call is required by the Web Form Designer
    > > 'Do not modify it using the code editor.
    > > InitializeComponent()
    > > End Sub
    > >
    > > #End Region
    > >
    > > Protected WithEvents ddlNames As

    System.Web.UI.WebControls.DropDownList
    > > Protected WithEvents lblResult As System.Web.UI.WebControls.Label
    > > Protected WithEvents btnRun As System.Web.UI.WebControls.Button
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > > 'Put user code to initialize the page here
    > > End Sub
    > >
    > > Private Sub Run(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles btnRun.Click
    > > ' None of these work :(
    > > lblResult.Text = ddlNames.SelectedValue
    > > lblResult.Text = ddlNames.SelectedValue.ToString
    > > lblResult.Text = ddlNames.SelectedItem.Text
    > > End Sub
    > >
    > > End Class
    > >
    > > ASPX CODE
    > >
    > > <%@ Page Language="vb" AutoEventWireup="false"

    Codebehind="WebForm1.aspx.vb"
    > > Inherits="dropdownlist.WebForm1"%>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > > <html>
    > > <head>
    > > <title>WebForm1</title>
    > > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > > <meta name=vs_defaultClientScript content="JavaScript">
    > > <meta name=vs_targetSchema
    > > content="http://schemas.microsoft.com/intellisense/ie5">
    > > </head>
    > > <body MS_POSITIONING="GridLayout">
    > >
    > > <form id="Form1" method="post" runat="server">
    > > <h1> Drop Down List Example </h1>
    > > </br>
    > > </br>
    > >
    > > <asp:DropDownList ID="ddlName" Runat="server">
    > > <asp:ListItem Selected="True"
    > > Value="Bill">Bill</asp:ListItem>
    > > <asp:ListItem Value="Jason">Jason</asp:ListItem>
    > > <asp:ListItem Value="Tim">Tim</asp:ListItem>
    > > </asp:DropDownList>
    > >
    > > </br>
    > > </br>
    > >
    > > <asp:Button ID="btnRun" Runat="server"

    Text="Run"></asp:Button>
    > > </br>
    > > </br>
    > >
    > > <asp:Label ID="lblResult" Runat="server"> </asp:Label>
    > > </form>
    > >
    > > </body>
    > > </html>
    > >
    > >
    > >
    Stephen Adam, Nov 30, 2004
    #4
    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. Replies:
    2
    Views:
    4,016
    Roedy Green
    Oct 2, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,001
    weiwei
    Jan 5, 2007
  3. KJ
    Replies:
    2
    Views:
    154
  4. pardesiya
    Replies:
    7
    Views:
    295
    pardesiya
    May 22, 2007
  5. Replies:
    0
    Views:
    162
Loading...

Share This Page