gpt4 book ai didi

c# - 重载的 RadTextBox 控件中的服务器端自定义验证器不调用其客户端验证函数

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:34 24 4
gpt4 key购买 nike

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

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

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

服务器端验证工作正常,但是我希望使用客户端验证函数来防止返回无效时的回发。有什么理由不调用客户端验证函数吗?

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 控件的值进行比较。

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

关于c# - 重载的 RadTextBox 控件中的服务器端自定义验证器不调用其客户端验证函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13459701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com