Monday, March 26, 2012

Propose: Validator Callout on DropDownList

in my opinion it would be very useful if validator callout targetcontrol could be not only a textbox but a dropdownlist too.

It could be appear if dropdownlist is selected on selectedindex= 0 or with a selectedvalue=""

Hi Maxredd,

As far as I know, it is supported. You can use a CustomValidator + ValidatorCalloutExtender. When postback , CustomValidator will call ClientValidationFunction to check the DropDownList's state. Here is the sample.

<%@. Page Language="C#" %><%@. Import Namespace="System.Collections.Generic" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e) { List<string> myList = new List<string>(); myList.Add("Select"); myList.Add("1111"); myList.Add("2222"); myList.Add("3333"); DropDownList1.DataSource = myList; DropDownList1.DataBind(); }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> <asp:CustomValidator ID="cvName" Runat="server" ClientValidationFunction="validateName" ErrorMessage="Please select an item!" Display="None" ControlToValidate="DropDownList1"></asp:CustomValidator> <asp:Button ID="btnSubmit" Runat="server" Text="GO"></asp:Button> <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" BehaviorID="myVCEBID" runat="server" TargetControlID="cvName"> </ajaxToolkit:ValidatorCalloutExtender> <script type="text/javascript" language="javascript"> function validateName(source, args){ if( $get("<%=DropDownList1.ClientID%>").selectedIndex == 0 ) args.IsValid = false; else args.IsValid = true; } </script> </form></body></html>

Best regards,

Jonathan


thank you!

No comments:

Post a Comment