Cannot have multiple items selected in a DropDownList.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Watch this example on YouTube:
Here is my HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>ONE</asp:ListItem>
<asp:ListItem>TWO</asp:ListItem>
<asp:ListItem>THREE</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
And here is my code behind
protected void Page_Load(object sender, EventArgs e)
{
ListItem lst = DropDownList1.Items.FindByText("ONE");
if (lst != null)
{
lst.Selected = true;
}
}
To fix it replace
lst.Selected = true;
With
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("ONE"));
No comments:
Post a Comment