How to get the 'controlToValidate' property on ClientValidationFunction?

此生再无相见时 提交于 2019-11-27 05:51:37

问题


Lets say I have this code.

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

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

And a validationFunction:

function ValidationFunction1(sender, args)
{
}

And i would like to know if, inside the function I could get the Control to validate something like:

var v = sender.ControlToValidate;

回答1:


Actually sender.controltovalidate gives the ClientID of the control. So this seems like a solution.

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

I tried and it worked for me. Please notify if it works.




回答2:


Not verified, just a hint:

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

of course you could simply do it like:

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

if you know exactly what you're validating. The first method is good when the control to be validated is set dynamically and you don't know beforehand which one it will be.

Also FindControl() might return null so you'd need to test for that too in order to avoid an exception.

Hope this helps.




回答3:


Here's my take on a server-side solution in C# to mimic the above answer, for anyone interested:

<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>

And on the server:

//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;
    }

These two lines are the crux of it.

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

The NamingContainer will be a GridViewRow in my case, but it could be your entire page depending on your program. Either way it allows me to find the control I want, relative to the ControlToValidate object, which as mentioned will return the ClientID.



来源:https://stackoverflow.com/questions/3636228/how-to-get-the-controltovalidate-property-on-clientvalidationfunction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!