By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,935 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

dropdownlist default selected

P: n/a
How do i make a dropdownlist selected value based on the value i retrive from
the database.

Basically i have an edit page and like to display the default value in a
dropdown list from the database.

for example: if the ddl_value is 2 from the database i want the second list
item to be selected by default.
<asp:DropDownList id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
selected
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownList>

I am using the dropdownlist inside a repeater.. and only retriving single
record at a time based on a querystring... and using a datareader to fill the
form.

Many thanks in advance..
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

You need to find the item that has the text value "value 2" and then get its
index value. Once you have the index value, you can set the SelectedIndex to
that value.

Here's a quick way to accomplish all three:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
ddl.SelectedIndex = ddl.Items.IndexOf _
(ddl.Items.FindByText("value 2"))
End If
End Sub

<asp:DropDownList id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem>
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownList>

Does this help?

Ken
Microsoft MVP [ASP.NET]

"huzz" <hu**@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
How do i make a dropdownlist selected value based on the value i retrive
from
the database.

Basically i have an edit page and like to display the default value in a
dropdown list from the database.

for example: if the ddl_value is 2 from the database i want the second
list
item to be selected by default.
<asp:DropDownList id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
selected
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownList>

I am using the dropdownlist inside a repeater.. and only retriving single
record at a time based on a querystring... and using a datareader to fill
the
form.

Many thanks in advance..


Nov 18 '05 #2

P: n/a
You have to use either FindByValue() or FindByText() method of the items
collection. If found, it returns the ListItem otherwise, the method returns
null.

ListItem item = MyDropDown.items.FindByValue("TX");
if(item != null)
{
MyDropDown.SelectedItem.Selected = false;
item.Selected = true;
}

Here is the reference link to the FindByValue and FindByText methods
http://msdn.microsoft.com/library/de...valuetopic.asp

HTH
-Chris
~
http://weblogs.austinspad.com/caustin

"huzz" <hu**@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
How do i make a dropdownlist selected value based on the value i retrive from the database.

Basically i have an edit page and like to display the default value in a
dropdown list from the database.

for example: if the ddl_value is 2 from the database i want the second list item to be selected by default.
<asp:DropDownList id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
selected
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownList>

I am using the dropdownlist inside a repeater.. and only retriving single
record at a time based on a querystring... and using a datareader to fill the form.

Many thanks in advance..

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.