473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Get Value of Selected Item From Drop Down List

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>
Nov 18 '05 #1
3 26248
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>

Nov 18 '05 #2
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" <Mr****@discussions.microsoft.com> wrote in message
news:41**********************************@microsof t.com...
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>

Nov 18 '05 #3
Twas a spelling mistake!
"MrMike" <Mr****@discussions.microsoft.com> wrote in message
news:41**********************************@microsof t.com...
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>

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Serge Myrand | last post by:
Hi, I cannot get the selected option to be POSTed (it does not appear in the QueryString when using GET neither) I construct the drop down list from an array, I select an element in the list,...
4
by: charliewest | last post by:
I need to set the selected drop down list value at run time. I am aware of the method "SelectIndex" however this works only if you know the precise location of the value within the ListItem...
1
by: darrel | last post by:
I'm binding data to a drop down list. Then, via another query, selecting one of the items from that list by default. What I'd like to do is change the text of that particular item as well. Can...
5
by: TB | last post by:
Hi All: The following is probably a newbie question, but please bear with me: I am populating a drop down list with items from a database, and would the default selected item to be the current...
0
by: weiwei | last post by:
Hi here is my scenario, I create a drop down list in itemtemplate.(that drop down is created from db), after user click edit command, my ideal plan is have another drop down list in...
3
by: Gurpreet22 | last post by:
I have designed a web form, which has a drop down list. What i would like to do is when a user selects a value from the drop down list and submits the form, the user is redirected to the next page,...
5
by: tsunet | last post by:
HI.. I wana display that, initially in a one drop down control there will be some values. Initially, 2nd drop down control will be disabled. if user selects 1st drop down then and then only value...
1
by: tsunethere | last post by:
hi .. I have 2 drop-down boxes. at first when web page will get load value this drop-down value will be"---" in this fashion. and 2nd drop-down will be disabled. unless and until user selects...
5
by: sharmilah | last post by:
Hi All I wish to use a drop down list with the values 'Active' and 'Inactive' and select one of those values to go into my table 'users' which has a field called 'status'. I do not want to store...
3
by: zzzxxxyyy | last post by:
i ve 2 dynamic drop down lists... after the user chooses a value in the first drop down ...the second list gets populated..this is done thru reloading... my prob is after reloading it once ,the value...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.