dropdownlist default selected

G

Guest

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..
 
K

Ken Cox [Microsoft MVP]

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]
 
C

Chris Austin

Joined
Feb 18, 2009
Messages
1
Reaction score
0
solution to this issue

hello all,
The solution is to add OnDataBound="SomeFunction" in your aspx (or ascx) file.

a sample dropdownlist would be

<asp: DropDownList ID="ddlPages" runat="server" DataSourceID="SqlDsListPagesOfThisModule"
DataTextField="ModulePageTitle" DataValueField="ModulePageId"
AutoPostBack="True" OnDataBound="RamOne" OnSelectedIndexChanged="ddlPages_SelectedIndexChanged" >
</asp: DropDownList>
//note that asp :dropdownlist DOES NOT contain spaces but the post replaces it with a smiley
then in the code behind

protected void RamOne(System.Object sender, System.EventArgs e)
{

if (!Page.IsPostBack)
{

if (ddlPages.Items.Count > 0)
{
ddlPages.SelectedIndex = 0;//the default value is -1 for nothing selected
//selectedindexchanged will NOT be triggered automatically
//so we trigger it (which is exactly what we need !!!!)
ddlPages_selectedindexchanged(sender,e);


}

}


}


protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
//force showing the list of dbinteractions on first load (!postback, before page load because this is called from ondatabaound)
if (!Page.IsPostBack)
{
something.databind();//for example Gridview1 or dropdownlist2 etc
}
else
{
//put here what you would normally put, for example something.databind()
//and for example a call to a function that logs preferences in a db for this user ("update memberprefs set dropdownvalue= "+ddlPages.selectedvalue+"...where userid=...");
}
}


good luck coding!!!
Eng. Ramzi Hawa
Database SqL 2005 expert, Dnn expert...
email ramzi at charlottecoders.com
website rolamzi.com
 
Last edited:

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top