Reputation: 31
I am using Telerik's RadControls for ASP.NET AJAX.
I'm attempting to create a custom control that contains all the functionality of a RadTextBox, but with the addition of doing an HTML Encode on the text to ensure that users cannot do any HTML injection or accidentally cause application errors. In PageLoad, the text is set to its encoded value, so that when we need to retrieve the data or store it in the database, it is in encoded format. I then set the textbox's text value to the decoded value for the sake of aesthetics.
I am attempting to add a custom validator that ensures the HTML Encode process does not accidentally expand the textbox's character count past the character limit of the field and/or database (Characters such as '<' or '>' are transformed into '<' and '>', which could extend past a given character limit).
The server-side validation works fine, however I wish to use the client validation function to prevent a postback if it returns invalid. Is there any reason that the client validation function would not be called?
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);
}
}
Upvotes: 2
Views: 790
Reputation: 9215
I believe this.ID
is not valid in the Init
event. Debug the value of that, and compare it to the value of the generated html control.
Setting cvTextBox.ControlToValidate = this.ID;
in your Load
method would also test this theory.
Upvotes: 0