views:

866

answers:

3

Hi there,

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.

Can anybody tell me how to stop ASP.NET from that conversion?

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 "&#".

A: 

You can just decode the text before inserting it in the database:

string htmlEncodedText = myTextbox.Text;
string htmlDecodedText = HttpUtility.UrlDecode(htmlEncodedText);

Encoding latinEncoding = Encoding.GetEncoding("8859-1");
Encoding utfEncoding = Encoding.UTF8;
byte[] utfBytes = utfEncoding .GetBytes(htmlDecodedText);
byte[] latinBytes =
    System.Text.Encoding.Convert(utfEncoding, latinEncoding, utfBytes);
string latinString = latinEncoding.GetString(latinBytes);

// TODO: Insert latinString into database
Seb Nilsson
I'm sorry, the database must stay completely in latin1 characterset as well. No unicode or utf8 characters are allowed here
rs
A: 

I have accomplished this simply inheriting from Textbox and overriding the Text property. It currently has the attribute PersistenceMode.EncodedInnerDefaultProperty. You want to change it to InnerDefaultProperty:

[BindableAttribute(true, BindingDirection.TwoWay)]
[PersistenceModeAttribute(PersistenceMode.EncodedInnerDefaultProperty)]
public virtual string Text { get; set; }

If you want this to apply to all your textboxes, you can use tag mappings to do it.

Reference Material

More on Persistence Mode
More on Textbox.Text property
More on Tag Mapping

PortageMonkey
Thanks a lot! Your answer pointed me the right way,please see my answer below.
rs
Awesome!, Would you mind accepting my answer :)
PortageMonkey
A: 

I solved my problem (PortageMonkey showed me the way):

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("&#", "&#"));
}

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>

This works very well!

rs