Mainly this issue occurred when in dropdown value is already selected and next step we are again trying to select a dropdown value by FindByText or FindByValue using dropdown item's property.
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem Value="0">1</asp:ListItem>
<asp:ListItem Value="1">2</asp:ListItem>
<asp:ListItem Value="2">3</asp:ListItem>
</asp:DropDownList>
Below code raise an error “Cannot have multiple items selected in a DropDownList”
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
DropDownList1.SelectedValue = "0";
DropDownList1.Items.FindByValue("1").Selected = true;
//OR
//DropDownList1.Items.FindByText("2").Selected = true;
}
catch (Exception ex)
{
throw ex;
}
}
}
To overcome this issue clear dropdown selection before next selection of dropdown.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
DropDownList1.SelectedValue = "0";
DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue("1").Selected = true;
//OR
//DropDownList1.Items.FindByText("2").Selected = true;
}
catch (Exception ex)
{
throw ex;
}
}
}
No comments:
Post a Comment