假设我正在运行一个包含许多.aspx页面的站点,这些页面继承自名为FormPage.cs
的文件。创建这些页面时,其中许多页面使用System.Web.UI.WebControls.HiddenField
中的隐藏字段。现在,已经意识到这些隐藏字段的Value
属性需要如下所示:
public override string Value
{
get
{
return HttpUtility.HtmlDecode(base.Value);
}
set
{
base.Value = HttpUtility.HtmlEncode(value);
}
}
是否有可能在FormPage.cs
文件中修改HiddenField
的get和set方法而不创建从中继承的新类,因此我不必替换每个实例所有页面都HiddenField
?
答案 0 :(得分:0)
您可以向HiddenField控件添加扩展方法 - 它不需要继承才能工作。但解决方案也是有限的,因为您必须更改代码以默认引用扩展方法。
public static void SetValue(this HiddenField c, string text)
{
c.Value = HttpUtility.HtmlEncode(text);
}
public static string GetValue(this HiddenField c)
{
return HttpUtility.HtmlDecode(c.Value);
}