ASP Developer Network - Brought to you by Steve Radich and BitShop, Inc. - http://www.bitshop.com

Menu [hide]

ASPFAQDropBoxSelection

print
similar comment
Once you need to create page with prefilled values you can do it using static or dynamic methods.
In this article we discuss, how to select desired value to dropdown list.

Static Method


With this method you should generate static HTML with "selected" property set, by example:

<select id="Country">
<option selected="selected" value="1">USA</option>
<option value="2">Canada</option>
<option value="3">Other</option>
</select>


In this case user will see dropdown list with selected USA option.
Note: only one value can be selected in all list.

Dynamic method:


ASP.NET developers can use only one common property of DropDownList object.

ListControl.SelectedValue Property

It "gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value. " (MSDN Library)

So to select item you should know the value, the text won't help.
We found good solution to select item also by text.

We recommend using
ListItemCollection.FindByText Method

It "searches the collection for a ListItem with a Text property that contains the specified text. " (MSDN Library)

See our code (all comments see below):

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


Protected Sub Page_Onload(ByVal sender As Object, ByVal e As System.EventArgs)
'Set initial values to Labels
lbText.Text = DropDownList1.Items(0).Text
lbValue.Text = DropDownList1.Items(0).Value.ToString
End Sub

'Main function to proceed user request to change
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'String to be searched in Dropdown
Dim strSearch As String

'Turn off errors
lbError.Visible = False

'Gets user input text
strSearch = TextBox1.Text

'Checks if input valid
If strSearch <> "" Then
Try
'reset selected item to select new one
DropDownList1.SelectedItem.Selected = False

DropDownList1.SelectedValue = ""

'search user input in Dropdown and select it
DropDownList1.Items.FindByText(strSearch).Selected = True

'shows a new selected data
ChangeLabel()

Catch ex As Exception
'if not, shows error to the user
lbError.Visible = True
lbError.Text = "Incorrect text entered. Please type text according to the list below"
End Try
Else
'if not, shows error to the user
lbError.Visible = True
lbError.Text = "Please type any text according to the list below"
End If
End Sub

'Shows a new selected data in labels
Sub ChangeLabel()
lbText.Text = DropDownList1.SelectedItem.Text
lbValue.Text = DropDownList1.SelectedItem.Value.ToString
End Sub

'shows a new selected data in labels
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
ChangeLabel()
End Sub


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DropDownList Selection Example</title>
</head>
<body>
<form id="form1" runat="server" onload="Page_Onload">
<div>
<strong>DropDownList Selection Example:<br />
<br />
</strong>Type US State short text (i.e. MA)<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><strong> </strong>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" Select State " /><br />
<asp:Label ID="lbError" runat="server" Visible="False" Width="262px" ForeColor="Red"></asp:Label><br />
<br />
Example DropDownList:<br />
<asp:DropDownList ID="DropDownList1" runat="server" Width="143px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="0">Select Any Item</asp:ListItem>
<asp:ListItem value="1">AK</asp:ListItem>
'.......
<asp:ListItem value="51">WV</asp:ListItem>
<asp:ListItem value="52">WY</asp:ListItem>

</asp:DropDownList><br />
<br />
Selected Text:
<asp:Label ID="lbText" runat="server" Width="173px" ForeColor="Black"></asp:Label><br />
Value: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:Label ID="lbValue" runat="server" ForeColor="Black"></asp:Label><br />
<br />
</div>
</form>
</body>
</html>


You can see this code working online on http://aspdeveloper.net/Samples/ASPNETFAQ/DropBoxSelectionexternal link (cache)

Comments


As you see in code part in Sub Button1_Click method "FindByText(strSearch)":
DropDownList1.Items.FindByText(strSearch).Selected = True

It means we find in DropDownList1.Items collection an item with Text=strSearch.
Then we set property Selected for the item as True.

You should use Try ... Catch block in case if method won't find any item with your text.

Created by: bitshopeugene last modification: Monday 28 of November, 2005 [14:11:56 UTC] by bitshopeugene



Search
in:
Our Sponsors
Virtual Server 2005 Info Here!
http://www.aspdeveloper.net
Need a Virtual Server 2005 Hosted Account? Why not trust ASPDeveloper's Team? We're a division of www.bitshop.com
Want to contribute to aspdeveloper.net? Contact us! suggest@aspdeveloper.net
FREE .Net 2.0 Tutorials Available Now
http://www.aspdeveloper.net
Virtual Server 2005 Info Here!
http://www.aspdeveloper.net
rss Wiki rss Blogs rss Articles rss Image galleries rss File galleries rss Forums rss Maps rss Directories
ASP Developer Network (since 1996) - Brought to you by Steve Radich and BitShop, Inc. - http://www.bitshop.com
Copyright © 1997-2005 by BitShop, Inc. All Rights Reserved.
No content may be reproduced in any form without written permission from BitShop, Inc.
We will be honored to assist with reproduction rights on our material if you contact us.
We also can provide print articles for your magazine, private training seminars at your site, and conference presentations.

Thank you for visiting. Please bookmark our site if you found it useful!
If you wish to make a donation it will help further the development of these free resources: