Showing posts with label CustomValidator with dropdown list. Show all posts
Showing posts with label CustomValidator with dropdown list. Show all posts

Tuesday, November 29, 2011

How to use CustomValidator with dropdown list


Here is an example of using CustomValidator with  dropdown list in asp.net application

Step 1 : Write  java script function
< script type="text/javascript" language="javascript" >

function CheckWorker(sender, args) {
                var worker = $get('<%=ddlstWorkers.ClientID %>');
                if (worker.selectedIndex == 0) {
                    args.IsValid = false;
                    return;
                }
                args.IsValid = true;
            }
< / script>

Step 2: Define the asp:DropDownList and CustomValidator

< asp:DropDownList ID="ddlstWorkers" runat="server" CssClass="ddl" Style="width: 220px;">
                                        < /asp:DropDownList >
                                        < asp:CustomValidator ID="cvWorker" runat="server" ControlToValidate="ddlstWorkers"   Display="None" ErrorMessage="Select a worker." ClientValidationFunction="CheckWorker" >< /asp:CustomValidator >