Setting DropDownlist Selected Value from Code Behind in ASP.NET C#

ASP.NET Comments Off

If you have a DropDownList control with some values in your .aspx page you can pre select any values in your code behind file. It can be selected either seting the SelectedIndex property or SelectedValue property.

.aspx page:

<asp:DropDownList ID="DropDownList1" runat="server">
 <asp:ListItem>Good</asp:ListItem>
 <asp:ListItem>Bad</asp:ListItem>
 <asp:ListItem>Ugly</asp:ListItem>
 </asp:DropDownList>

Code Behind .CS file:


///By setting the Index (remember index starts with 0)

DropDownList1.SelectedIndex = 0;

/// By setting the Value

DropDownList1.SelectedValue = "Ugly";

For the first case you will get Good as the selected value. For the second case you will get Ugly as the selected value.



Author

Search

Back to Top