How to validate a dropbox

Aug 29, 2014 at 9:48 PM
Hi ,
How to validate a drop box using DAvalidation

Regards
Arun A
Coordinator
Sep 1, 2014 at 9:53 PM
Edited Sep 1, 2014 at 9:54 PM
I'm not sure what is your issue. In case you want to validate that user selected something make a default value an empty string and then property marked with RequiredAttribute should work just fine. Otherwise you'll need to write your own validation attribute and a JavaScript function as shown in the last part of the article about the internals of the validator: http://amanek.com/building-data-annotations-validator-control-with-client-side-validation/.

Model class:
[Required]
public string Foo { get; set; }
Markup
<asp:DropDownList runat="server" ID="ddlFoo">
    <asp:ListItem Text="Select" Value=""></asp:ListItem>
    <asp:ListItem Text="Quux" Value="1"></asp:ListItem>
    <asp:ListItem Text="Bat" Value="2"></asp:ListItem>
    <asp:ListItem Text="Baz" Value="3"></asp:ListItem>
</asp:DropDownList>
<dav:DataAnnotationsValidator runat="server" ID="davFoo"
    MetadataSourceID="MetadataSource1"
    ControlToValidate="ddlFoo"
    ObjectProperty="Foo" />