468,243 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,243 developers. It's quick & easy.

dropdownlist default selected

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
2 11087
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
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.

Similar topics

3 posts views Thread by Olivier Verdin | last post: by
1 post views Thread by Frank | last post: by
1 post views Thread by RichardH | last post: by
1 post views Thread by clickon | last post: by
4 posts views Thread by rn5a | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.