簡體   English   中英

如何獲取 ClientValidationFunction 上的“controlToValidate”屬性?

[英]How to get the 'controlToValidate' property on ClientValidationFunction?

假設我有這個代碼。

<asp:TextBox ID="TextBox1" runat="server" />

<asp:CustomValidator ID="CustomValidator1" runat="server"
    ClientValidationFunction="ValidationFunction1"
    ControlToValidate="TextBox1"
    Display="Dynamic" />

還有一個validationFunction:

function ValidationFunction1(sender, args)
{
}

我想知道,在函數內部,我是否可以讓 Control 驗證以下內容:

var v = sender.ControlToValidate;

實際上sender.controltovalidate給出了控件的ClientID 所以這似乎是一個解決方案。

function ValidationFunction1(sender, args){
    var v = document.getElementById(sender.controltovalidate);
}

我試過了,它對我有用。 請通知是否有效。

未驗證,僅提示:

var v = document.getElementById('<%=CustomValidator1.FindControl(CustomValidator1.ControlToValidate).ClientID>%');

當然,你可以簡單地這樣做:

var v = document.getElementById('<%=TextBox1.ClientID%>');

如果您確切地知道要驗證的內容。 當要驗證的控件是動態設置的並且您事先不知道它將是哪個控件時,第一種方法很好。

FindControl()也可能返回null因此您也需要對此進行測試以避免異常。

希望這會有所幫助。

這是我對 C# 中的服務器端解決方案的看法,以模仿上述答案,對於任何感興趣的人:

<asp:TextBox ID="txtStudentComments" runat="server" 
  Rows="8" Width="100%" 
  ToolbarCanCollapse="False" ValidationGroup="vg1" />
<asp:CustomValidator ID="cv1" runat="server" ControlToValidate="txtStudentComments" 
ErrorMessage="THESE COMMENTS DO NOT SEEM RIGHT. PLEASE REVIEW THEM AGAIN!" SetFocusOnError="true" 
Font-Bold="True" Font-Size="Medium" ValidationGroup="vg1" OnServerValidate="cv1_ServerValidate"></asp:CustomValidator>

在服務器上:

//validate of the comment contains some specific words which imply the TET has not reviewed the comments!
    protected void cv1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        CustomValidator cv = (CustomValidator)source;
        GridViewRow gvRow = (GridViewRow)cv.NamingContainer;
        TextBox editor = (TextBox)gvRow.FindControl("txtStudentComments");

        if (editor.Text.ToUpper().Contains("FACILITATOR TO INSERT COMMENTS HERE PLEASE"))
            args.IsValid = false;
        else
            args.IsValid = true;
    }

這兩行是重點。

    CustomValidator cv = (CustomValidator)source;
    GridViewRow gvRow = (GridViewRow)cv.NamingContainer;

NamingContainer 在我的例子中將是一個 GridViewRow,但它可能是你的整個頁面,具體取決於你的程序。 無論哪種方式,它都允許我找到我想要的控件,相對於 ControlToValidate 對象,如前所述,它將返回 ClientID。

這是我能夠訪問控件以在客戶端進行驗證的簡單解決方案。 使用您可能需要的選項添加常規自定義驗證器控件。

<asp:CustomValidator ID="cvalShippingRegionCountries" ErrorMessage="Choose a country" ClientValidationFunction="ClientValMultiSelectCountries" runat="server" Display="Dynamic" SetFocusOnError="true" /> 

然后,在后面的代碼中,只需添加一個自定義屬性來存儲要驗證的控件的 clientID。

cvalShippingRegionCountries.Attributes.Add("ControlToValidateClientID", multiselectShippingRegionCountries.ClientID);

現在,在處理驗證的函數中,您可以像這樣訪問值:

function ClientValMultiSelectCountries(sender, args) {

        var multiselect = $find(sender.attributes.controltovalidateclientid.nodeValue);

        if ( #VALIDATION_CHECK_HERE# ) {
            args.IsValid = false;
        }
    }

您將在函數中獲得 clientID ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM