The ASP.NET do and don’ts serie : episode 3 – use the validator designed for your needs !

This post is relative to the first episode of the “ASP.NET do and don’ts serie”

ASP.NET contains several validation controls. Be sure to use the good ones for your needs.

Imagine you have the following dropdownlist

<asp:DropDownList runat="server" ID="MyDDL"
      // datasource params
     AppendDataBoundItems="true">
     <asp:ListItem Text="select a value" Value="-1" Selected="True" />
</asp:DropDownList>

Do:


<asp:RequiredFieldValidator runat="server" ID="MyDDLValidator" ControlToValidate="MyDDL" ErrorMessage="Selection required" InitialValue="-1" />

Don’t:


<asp:CompareValidator runat="server" ID="MyDDLValidator"
 ValueToCompare="-1" ControlToValidate="MyDDL"
 Operator="NotEqual" ErrorMessage="Selection required" />

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>