【发布时间】:2016-08-27 05:48:22
【问题描述】:
我有一个使用 aspx 页面的 Web 应用程序。
首先,我想在 LabelledTextBox 中显示值时使用 Server.HtmlEncode(value)
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);
}
}
}
值<script>alert("hello")</script>已显示,但脚本已执行。
之后,我想尝试另一种处理异常的解决方案
A potentially dangerous Request.Form value was detected from the client
停留在包含表单的同一页面上,并在顶部显示一条错误消息,其中包含一条通用消息,例如“请确保所有输入不包含''之类的字符”
解决方案 1:我做错了什么?
解决方案 2:如何处理此异常并与填写的表单保持在同一页面上
一般:哪种解决方案最好?
谢谢!
【问题讨论】:
标签: asp.net validation exception xss html-encode