I have a web application using aspx pages.
First, I wanted to use Server.HtmlEncode(value)
whhen displaying the value in a LabelledTextBox
public interface ILabelledControl
{
bool ReadOnly { get; set; }
}
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class LabelledTextBox : TextBox, ILabelledControl
{
//public Unit EditableWidth { get; set; }
public Unit ReadOnlyWidth { get; set; }
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if (this.ReadOnly)
{
System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
foreach (string att in this.Attributes.Keys)
lbl.Attributes.Add(att, this.Attributes[att]);
lbl.Text = this.Text;
lbl.ForeColor = ForeColor;
//lbl.Width = this.Width;
if (ReadOnlyWidth != null)
lbl.Width = ReadOnlyWidth;
lbl.CssClass = CssClass;
lbl.ID = this.ID;
lbl.RenderControl(writer);
}
else
{
base.Render(writer);
}
}
}
The value <script>alert("hello")</script>
was displayed but the script was executed.
Afterward, I wanted to try another solution which was to handle the exception
A potentially dangerous Request.Form value was detected from the client
to stay on the same page containing the form and displaying an error message on the top with a generic message like "Please be sure that all input does not contain characters like '<' or '>'"
Solution 1 : What am I doing wrong ?
Solution 2 : How can I handle this exception and stay on the same page with the filled in form
General : Which solution is the best ?
Thx !