I have a dropdownlist and it is very simple. You click the button to go further you have error if you don’t select nothing from DDL. But how i do to not appear the error when the user choose something from the DDL?
Here is the markup:
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="jump">
<asp:ListItem Text="" Value="0"></asp:ListItem>
<asp:ListItem Text="Coupé" Value="Coupé" />
<asp:ListItem Text="Cabriolet" Value="Cabriolet" />
<asp:ListItem Text="Pickup" Value="Pickup" />
<asp:ListItem Text="Kombi" Value="Kombi" />
<asp:ListItem Text="Kombi-Coupé" Value="Kombi-Coupé" />
<asp:ListItem Text="Minibuss" Value="Minibuss" />
<asp:ListItem Text="Sedan" Value="Sedan" />
<asp:ListItem Text="Skåpbil" Value="Skåpbil" />
<asp:ListItem Text="SUV" Value="SUV" />
<asp:ListItem Text="Övrig" Value="Övrig" />
</asp:DropDownList>
<p id="err" runat="server"></p>
and code-behind:
if (DropDownList1.SelectedIndex == 0)
{
err.InnerHtml = "<span style='font: normal bold 11px/100% Arial, Verdana, Sans-Serif; color: Red'>Du måste välja Fordonstyp!</span>";
return;
}
Thank you so much in advance
As simple as this:
This will hide the error by default, then show it only if needed.
Edit: to support more than one drop down wrap it nicely with a function first:
Now just use the same error label to show error for any drop down, and have such code in the
Page_Load: