Get Value of Selected Item From Drop Down List


S

Stephen Adam

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>
 
Ad

Advertisements

G

Guest

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
 
S

Stephen Adam

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
 
Ad

Advertisements

S

Stephen Adam

Twas a spelling mistake!
MrMike said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top