Tuesday, 14 June 2011

How to Set the Default Value of a DropDownList in an ASP.NET


DropDownList Control Examples:
You can see the live samples and examples of DropDownList Control from the following links:
  • Setting DropDownList Default Value
  • DropDownList Control SelectedValue Property
  • DropDownList Control SelectedIndex Property
  • DropDownList Control JavaScript Validation
  • Populate DropDownList Items Dynamically
The second type of situation occurs when you are updating the old data then you have to display the default value of DropDownList control as previously saved value in the database. You can use the following code to set the list item as default selected value for dropdownlist control:



Example 1:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("Confections"));
Example 2:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("3"));
Example 3:
DropDownList1.Items.FindByValue("4").Selected = true;



<asp:DropDownList ID="ddlLanguage"runat="server"Style="width: 200px">
asp:DropDownList>

ddlLanguage.SelectedIndex = ddlLanguage.Items.IndexOf(ddlLanguage.Items.FindByText("English"));



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger