2009-06-26 56 views
4

我試圖驗證在轉發器中的產品qty文本框中輸入了某個產品的某個增量。問題是每個產品的增量都不一樣,所以我需要它作爲每個調用的變量來驗證它(我認爲您不能使用自定義驗證程序),並且我需要使用ValidatorCalloutExtender 。我提出的最佳解決方案是觸發一個RegEx驗證器,通過我自己的javascript評估false(另一個驗證器負責確保其有效數字)。問題是,使用ValidatorCalloutExtender,當我禁用驗證器時,它仍然將其標記爲無效(文本框閃爍白色,然後再次變爲黃色(即無效),即使我放置了JavaScript警報,但我知道驗證器正在禁用。有任何想法,以什麼怎麼回事下面是代碼謝謝禁用驗證程序但驗證程序標註仍顯示並導致驗證

PS:?!一切正常瓦特/出validatorCalloutExtender,但我真的需要標註擴展

的驗證:

<asp:RegularExpressionValidator ID="ProductIncrementValidator" runat="server" 
    ControlToValidate="ProductQtyTxt" 
    ErrorMessage="Please enter a valid increment" 
    ValidationExpression="^triggerthisvalidation$" 
    Enabled="false" 
    Display="Dynamic" 
    SetFocusOnError="true" 
    ValidationGroup="productValidation"> 
</asp:RegularExpressionValidator> 

<ajax:ValidatorCalloutExtender ID="ProductIncrementVE" runat="server" 
    TargetControlID="ProductIncrementValidator" 
    HighlightCssClass="validator" 
    WarningIconImageUrl="~/img/blank.gif"> 
</ajax:ValidatorCalloutExtender> 

數據綁定產品時:

Dim productQtyTxt As TextBox 
productQtyTxt = CType(e.Item.FindControl("ProductQtyTxt"), TextBox) 

Dim incrementValidator As RegularExpressionValidator 
incrementValidator = CType(e.Item.FindControl("ProductIncrementValidator"), RegularExpressionValidator) 
incrementValidator.ErrorMessage = "Please enter an increment of " & product.OrderIncrement.ToString() 


' Add item qty increment check 
productQtyTxt.Attributes.Add("onChange", "javascript:checkIncrement('" _ 
    & productQtyTxt.ClientID & "', " _ 
    & product.OrderIncrement & ", '" _ 
    & incrementValidator.ClientID & "')") 

的JavaScript:對於ValidatorCalloutExtender

function checkIncrement(textboxID, incrementQty, validatorID) { 
    var textbox = $get(textboxID); 
    var incrementValidator = $get(validatorID); 
    var qtyEntered = textbox.value; 

    if ((qtyEntered % incrementQty) != 0) { 
     ValidatorEnable(incrementValidator, true); 
     alert("not valid"); 
     return; 
    } 
    else { 
     ValidatorEnable(incrementValidator, false); 
     alert("valid"); 
     return; 
    } 
} 
+0

我將文本框的CSS類設置爲javascript函數中的空白並隱藏了標註,所以現在可以使用,但是在框上設置焦點時,它會調用驗證並嘗試針對RegEx驗證程序的^ triggerthisvalidation $表達式進行驗證。我如何使它不會觸發驗證焦點? – Ryan 2009-06-26 16:56:32

回答

0

我有同樣的問題,我解決了這樣的事情

if ((qtyEntered % incrementQty) != 0) { 
    ValidatorEnable(incrementValidator, true); 
    $("#" + validatorID + "_ValidatorCalloutExtender_popupTable").show(); 
    alert("not valid"); 
    return; 
} 
else { 
    ValidatorEnable(incrementValidator, false); 
    $("#" + validatorID + "_ValidatorCalloutExtender_popupTable").hide(); 
    alert("valid"); 
    return; 
} 

希望這可以幫助別人。

1

1.設置CSS類:


<style id = "style1" type="text/css"> 
     .CustomValidator 
     { 
      position: relative; 
      margin-left: -80px; 
      margin-top: 8px; 
      display: inherit; 
     } 
</style> 
 

 

<ajax:ValidatorCalloutExtender ID="ProductIncrementVE" runat="server" 
    TargetControlID="ProductIncrementValidator" 
    HighlightCssClass="validator" 
    WarningIconImageUrl="~/img/blank.gif" 
    CssClass="CustomValidator"> 
</ajax:ValidatorCalloutExtender> 

2。在需要時使用JavaScript來改變這個CSS類。集顯=無:

 


function alterDisplay(type) { 
    var styleSheet, cssRule; 
    if (document.styleSheets) { 
     styleSheet = document.styleSheets[index1]; 
     if (styleSheet) { 
      if (styleSheet.cssRules) 
       cssRule = styleSheet.cssRules[index2]; // Firefox 
      else if (styleSheet.rules) 
       cssRule = styleSheet.rules[index2];   // IE 
      if (cssRule) { 
       cssRule.style.display = type; 
      } 

     } 
    } 
} 
 

注:的索引1和索引2可從網頁不同,它是由你的宣言。您可以使用IE調試器來查找我們正確的索引。