如何保持ASP.NET TextBox从HTML编码其内容? [英] How do I keep an ASP.NET TextBox from HTML-encoding its content?
问题描述
问题是我在FormView中有一个ASP.NET TextBox,其Text属性绑定到数据库表字段。在某些情况下,可能有HTML代码与数据库中的正常文本相结合。 RequestEncoding和ResponseEncoding设置为iso-8859-1(whitch为latin1)。有一些字段可以组合latin1字符和西里尔字符。这在输入西里尔字符(例如д)时没有问题。因为RequestEncoding设置为iso-8859-1和浏览器的字符集,浏览器将更改为д,将保存在数据库中。这是我想要的,但因为д将转换为& amp;#1076;当将响应输出发送到浏览器之前设置为ASP.NET TextBox的Text属性时,我会看到д
the "problem" is that I do have an ASP.NET TextBox in a FormView with its Text property bound to a database table field. In some cases, there might be HTML-code combined with normal text in the database. The RequestEncoding and ResponseEncoding is set to "iso-8859-1" (whitch is latin1). There are some fields where latin1-characters and cyrillic characters may be combined. This is no problem when typing in cyrillic characters (e.g "д"). Because the RequestEncoding is set to iso-8859-1 and the charset for the browser as well, the browser will change д to "д", which will be saved in the database. That's what I want, but because "д" will be converted to "д" when set to an ASP.NET TextBox's Text-property before the response output is sent to the browser, I do see "д" after saving the data.
任何人都可以告诉我如何停止ASP.NET的转换。
Can anybody tell me how to stop ASP.NET from that conversion?
:在所有控件都呈现之后,是否有办法改变ASPX网站的完整输出? - 我可以用&#替换& amp;#。
ALternatively: Is there a way to alter the conplete output of an ASPX site after all controls have ben rendered? - I could than replace "&#" by "&#".
推荐答案
我解决了我的问题显示我的方式):
I solved my problem (PortageMonkey showed me the way):
首先我创建了一个类IsoTextBox,它继承自System.Web.UI.WebControls.TextBox。因为Text属性总是返回一个非编码字符串,所以简单地改变Text属性属性没有做这个工作。我改写了Render方法:
First I created a class "IsoTextBox" which inherits from System.Web.UI.WebControls.TextBox. Because the Text property does always returns a non-encoded string, simply changing the Text properties attribute did not do the job. Instead I overrote the Render method:
protected override void Render(HtmlTextWriter writer)
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter source = new HtmlTextWriter(stringWriter);
base.Render(source);
writer.Write(stringWriter.ToString().Replace("&#", "&#"));
}
并且在web.config中添加了tagMapping:
And than added the tagMapping in the web.config:
<system.web>
<pages>
<tagMapping>
<add mappedTagType="MyNamespace.IsoTextBox" tagType="System.Web.UI.WebControls.TextBox"/>
</tagMapping>
</pages>
<globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1"/>
</system.web>
这效果很好!
这篇关于如何保持ASP.NET TextBox从HTML编码其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!