使用Javascript在ASP.net中获取自定义验证器控件的参数?

时间:2011-07-17 16:49:22

标签: javascript asp.net validation callback customvalidator

所以我有一个我正在使用ClientValidationFunction捕获它的事件。

我遇到的问题是,我正在用它做一些过程,这个过程的结果应该确定args.IsValid是真还是假。但我抓到的结果是在回调中,我似乎无法找到一种方法来访问args。

我已经定义了一个var Args globaly并尝试从那里获取值;无法得到它。

我试过通过userContext发送它;没用(认为它只接受字符串)。

我试图通过document.getElementById获取验证器,但我找不到args属性。这是我最合乎逻辑的那个,但我似乎无法找到它。

我写的javascript代码是这样的;

function ValidateUserMail(oSrc, args) {
    var myField = document.getElementById('<%=RegisterUserWizardStep.ContentTemplateContainer.FindControl("Email").ClientID %>');
    PageMethods.IsMailAvailable(myField.value, OnSucceeded); 
}

//i need to edit args value from this function below.
function OnSucceeded(result, userContext, methodName) {
    if (methodName == "IsMailAvailable")
        if (!result) {
            var myValidator = document.getElementById('<%=RegisterUserWizardStep.ContentTemplateContainer.FindControl("UniqueEmailRequired").ClientID %>');
            myValidator.args = false; //doesn't work any ideas about the working property?
        }
}

我在aspx for validator中编写的代码是这样的;

  <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" 
  CssClass="failureNotification" ErrorMessage="E-mail is required." ToolTip="E-mail is required." 
  ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>

  <asp:CustomValidator ID="UniqueEmailRequired" runat="server" ControlToValidate="Email"
  CssClass="failureNotification" ErrorMessage="Unique E-mail is required." ToolTip="Unique E-mail is required."
  ValidationGroup="RegisterUserValidationGroup" ClientValidationFunction="ValidateUserMail">*</asp:CustomValidator>

非常感谢!


更新;在浏览器中为uniqueEmailValidator Javascript渲染html;

    var MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired = document.all ? document.all["MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired"] : document.getElementById("MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired");
    MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired.controltovalidate = "MainContent_RegisterUser_CreateUserStepContainer_Email";
    MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired.errormessage = "Unique E-mail is required.";
    MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired.validationGroup = "RegisterUserValidationGroup";
    MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired.evaluationfunction = "CustomValidatorEvaluateIsValid";
    MainContent_RegisterUser_CreateUserStepContainer_UniqueEmailRequired.clientvalidationfunction = "ValidateUserMail";

2 个答案:

答案 0 :(得分:0)

args ValueIsValid中有两个属性
试试

    myValidator.args.Value = '3434';
    myValidator.args.IsValid = true or false ;

答案 1 :(得分:0)

试试这个:

function ValidateUserMail(oSrc, args) {
        var myField = document.getElementById('<%=RegisterUserWizardStep.ContentTemplateContainer.FindControl("Email").ClientID %>');
        PageMethods.IsMailAvailable(myField.value, function (result, userContext, methodName) { args.IsValid = result; });
    }