繁体   English   中英

重载的RadTextBox控件中的服务器端自定义验证器不会调用其客户端验证功能

[英]A server-side custom validator in an overloaded RadTextBox control does not call its client validation function

我正在使用Telerik的RadControls用于ASP.NET AJAX。

我试图创建一个自定义控件,其中包含RadTextBox的所有功能,但是还要对文本进行HTML编码,以确保用户不能进行任何HTML注入或意外导致应用程序错误。 在PageLoad中,文本被设置为其编码值,因此当我们需要检索数据或将其存储在数据库中时,其为编码格式。 然后,为了美观起见,我将文本框的文本值设置为解码后的值。

我正在尝试添加一个自定义验证器,以确保HTML编码过程不会意外地将文本框的字符数扩展到字段和/或数据库的字符数限制(诸如“ <”或“>”之类的字符转换为“&lt; '和'&gt;',它们可以超出给定的字符数限制)。

服务器端验证工作正常,但是我希望使用客户端验证功能来防止回传(如果返回无效)。 是否有任何原因不会调用客户端验证功能?

C# :

public partial class CustomTextBox : RadTextBox
{
    private CustomValidator cvTextBox;
    private string encodedText;
    private string decodedText;

    public CustomTextBox()
    {
        base.Init += Init;
        base.Load += Load;
        base.PreRender += PreRender;
    }

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            decodedText = HttpUtility.HtmlDecode(value);
            encodedText = decodedText == value ? HttpUtility.HtmlEncode(value) : value;
            base.Text = encodedText;
        }
    }

    protected new void Init(object sender, EventArgs e)
    {
        cvTextBox = new CustomValidator();
        cvTextBox.ID = this.ID + "_cvTextBox";
        cvTextBox.ControlToValidate = this.ID;
        cvTextBox.ClientIDMode = ClientIDMode.Static;
        cvTextBox.Display = ValidatorDisplay.Dynamic;
        cvTextBox.SetFocusOnError = false;
        cvTextBox.ErrorMessage = "<div class='validationMessage'>Field is too large after encoding. Remove any unnecessary '<', '>', or '&' characters.</div>";
        cvTextBox.ServerValidate += CustomTextBox_Validate;
        cvTextBox.EnableClientScript = true;
        cvTextBox.ClientValidationFunction = "testvalidate";
        cvTextBox.ValidateEmptyText = true;
        Controls.Add(cvTextBox);
    }

    protected new void Load(object sender, EventArgs e)
    {
        base.Text = encodedText;
    }

    protected new void PreRender(object sender, EventArgs e)
    {
        base.Text = decodedText;
    }

    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);
        cvTextBox.RenderControl(writer);
    }

    public void CustomTextBox_Validate(object source, ServerValidateEventArgs args)
    {
        CustomValidator cv = source as CustomValidator;
        args.IsValid = encodedText.Length < MaxLength;
    }
}

Javascript:

function testvalidate(source, args) {
    try {
        console.log("Will this message show?");
        args.IsValid = $find(source.controltovalidate).get_value().length < 50;
    } catch (error) {
        logError(error);
    }
}

我相信this.IDInit事件中无效。 调试该值,然后将其与生成的html控件的值进行比较。

设置cvTextBox.ControlToValidate = this.ID; 在您的Load方法中也将检验这一理论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM