2009-07-22 72 views
0

我有一個頁面,用戶可以通過下拉菜單選擇供應商或通過文本框輸入供應商編號。一方或另一方必須具有價值。我可以輕鬆地在javascript中執行此操作,但是如何使用ajax提供的自定義驗證器在客戶端執行此操作?驗證下拉列表或文本框

編輯

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click 
    If Page.IsValid Then 
     //save stuff 
    End If 
End Sub 

Sub ServerValidation(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles ValidPage.ServerValidate 

    Try 
     ' Test whether the value entered into the text box is even. 
     Dim num As Integer = Integer.Parse(args.Value) 
     args.IsValid = ((num Mod 2) = 0) 

    Catch ex As Exception 
     args.IsValid = False 
End Try 

End Sub 

Protected Sub ValidPage_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) 
    Try 
     args.IsValid = (Not String.IsNullOrEmpty(Me.txtVendnum.Text) OrElse Me.DropDownList1.SelectedIndex <> 0) 
    Catch e As Exception 
     DirectCast(source, CustomValidator).ErrorMessage = "You must Choose a Vendor" 
     args.IsValid = False 
    End Try 
End Sub 
<script type="text/javascript" language="javascript" > 
    function ClientValidate(sender, args) { 
     // Get Both form fields 
     var ddlvalue = document.getElementById('<%=DropDownList1.ClientID%>'); 
     var txtValue = document.getElementById('<%=txtVendnum.ClientID %>'); 

    // do you client side check to make sure they have something 
     if (txtValue.value == '' && ddlvalue.value == '0') { 

     args.IsValid = false; 
    } 
    else 
    { 
     args.IsValid = true; 
    } 

}

<asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="SqlDataSource1" ValidationGroup="Save" DataTextField="vmvnnm" DataValueField="vmvend" > 
        </asp:DropDownList> 

<asp:TextBox ID="txtVendnum" ValidationGroup="Save" runat="server"></asp:TextBox><br /> 

         <asp:CustomValidator ID="ValidPage" ValidationGroup="Save" runat="server" ClientValidationFunction="ClientValidate" ErrorMessage="CustomValidator" 
          SetFocusOnError="True" ControlToValidate="txtVendnum" EnableClientScript="true" Display="Static" OnServerValidate = "ServerValidation" ></asp:CustomValidator> 

//other stuff. A different validator group, html editor, etc 


<td> 
         <asp:ImageButton ID="ImageButton1" CausesValidation = "true"  ValidationGroup="Save" OnClick="ImageButton1_Click" runat="server" ImageUrl="../images/bttnSave.gif" /> 

        </td> 
+0

你可能要考慮做一個控件,一個文本框上的Ajax自動完成。有很多偉大的圖書館可以幫助您:(YUI,jQuery)(它屬於這裏對不起〜) – BigBlondeViking 2009-07-22 19:50:11

回答

1
<asp:CustomValidator ID="ValidPage" runat="server" 
    EnableClientScript="true" 
    ClientValidationFunction="My.util.VendnumCheck" 
    OnServerValidate="ValidPage_ServerValidate" 
    ControlToValidate="txtVendnum" 
    ErrorMessage="You must Choose a Vendor" 
    SetFocusOnError="True" 
    ValidationGroup="Save"> 
</asp:CustomValidator> 

ClientValidationFunction = 「My.util.VendnumCheck」

你也應該做服務器端驗證!

protected void ValidPage_ServerValidate(object source, ServerValidateEventArgs args) 
{ 
    try 
    { 
     args.IsValid = (!string.IsNullOrEmpty(this.txtVendnum.Text) || this.DropDownList1.SelectedIndex != 0); 
    } 
    catch (Exception e) 
    { 
     ((CustomValidator)source).ErrorMessage = "You must Choose a Vendor"; 
     args.IsValid = false; 
    } 
} 


protected void Button_Click(object sender, EventArgs e) 
{ 
    if (Page.IsValid) 
    { 
     //do work 
    } 
} 

JS:具有此呼籲文本框的模糊JS方法

My.util.VendnumCheck = function(sender, args) { 
try { 
     // Get Both form fields 
     var ddlvalue = document.getElementById("<%=this.DropDownList1.ClientID %>"); 
     var txtValue = document.getElementById("<%=this.txtVendnum.ClientID %>").value; 

     // do you client side check to make sure they have something 
     if (txtValue.length < 1 && ddlvalue.selectedIndex != 0) 
      args.IsValid = false; 

     args.IsValid = true; 
    } 
    catch (ex) { 
     My.logError(ex); 
     args.IsValid = false; 
    } 
} 

試試,看它是否拿起驗證...

My.util.callMyValidators = function() { 
    // Clean Up Infragistics Ids 
    var cleanid = this.id.replace(/^igtxt/i, ""); 

    if (Page_Validators == null) { 
     alert("My.util.callMyValidators when Page_Validators is null"); 
    } 
    var found = 0; 

    for (var i = 0; i < Page_Validators.length; i++) { 
     if (Page_Validators[i].controltovalidate === cleanid) { 
      found++; 
      ValidatorValidate(Page_Validators[i]); 
     } 
    } 

    if (found === 0) { 
     alert("My.util.callMyValidators did find any matching validators for " + cleanid); 
    } 
} 
1

您需要將您的自定義驗證的ClientValidationFunction屬性設置爲JavaScript函數將在頁面上存在名稱。

函數需要帶兩個參數,其中之一是您將設置爲有效或無效的「args」參數。一個例子based on the MSDN page for CustomValidator

function ClientValidate(source, args) 
    {   
     if (myTextBox.Value == "" && myDropDown.Value == "") 
     { 
     args.IsValid=false; 
     } 
     else {args.IsValid=true}; 
    } 
+1

您可能想要在文本框上做1個控件,一個Ajax自動完成。有很多偉大的圖書館可以幫助:(YUI,jQuery) – BigBlondeViking 2009-07-22 19:22:58

+0

+1。 – womp 2009-07-22 19:24:27