õ vs õ ( Character Encoding + Server.HtmlEncode problem)http://forums.asp.net/t/1096884.aspx/1?%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+Tue, 10 Apr 2007 18:42:05 -040010968841658069http://forums.asp.net/p/1096884/1658069.aspx/1?%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+õ vs õ ( Character Encoding + Server.HtmlEncode problem) <p>Hi everybody, this is a problem regarding <strong>Character Encoding</strong> and <strong>Server.HtmlEncode</strong>.<br> I hope someone can help me out here.<br> <br> <br> This is the situation.<br> <strong>1)</strong> The user enters <span style="font-weight:700; color:#f00">õ</span> into a TextBox control and my application saves it into the Database without any changes.<br> <strong>2)</strong> The user comes back and see &quot;Read Only&quot; version of what he entered. I do&nbsp;this which results in the encoded <span style="font-weight:700; color:#f00">&amp;#245;</span> on the web page.</p> <pre class="prettyprint">myLabelControl.Text = Server.HtmlEncode( databaseStoredColumnValue );</pre>&nbsp;<BR><STRONG>3)</STRONG> On top of that, when I go back into "Edit" mode, I do&nbsp;the following and have the same result as #2 above. <pre class="prettyprint">myTextBoxControl.Text = Server.HtmlEncode( databaseStoredColumnValue );</pre> <p><br> Any ideas how I can fix this? <br> I would really appreciate any help. <br> </p> <p>Thanks in advance,<br> Soe</p> 2007-04-09T22:23:52-04:001658095http://forums.asp.net/p/1096884/1658095.aspx/1?Re+%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem) <p>1) The line </p> <p>Label1.Text = Server.HtmlEncode(&quot;õ&quot;) </p> <p>Displays õ in place of the label just as it should.&nbsp; There may be something else contributing to the result you're getting for that, such as another tag or css attribute.&nbsp; </p> <p>2) Textboxes don't need their values to be HtmlEncoded.&nbsp; They display and return the unencoded string.&nbsp; The following line should work fine.</p> <p>myTextBoxControl.Text = databaseStoredColumnValue;</p> 2007-04-09T23:00:45-04:001659257http://forums.asp.net/p/1096884/1659257.aspx/1?Re+%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem) <p>The problem is that I am actually using my own Custom Web Control<br> which can switch between &quot;Text Input&quot; mode and &quot;Label&quot; mode.<br> <br> <br> I have the control behave like that so I don't need to keep <strong>2 Controls (1 TextBox and 1 Label)</strong> on the web page<br> to switch between &quot;Read Only&quot; and &quot;Edit&quot; modes.<br> <br> I just do either one of the following lines of code.</p> <pre class="prettyprint">myLabelText.TextMode = MyTextMode.Label; // go to read-only mode myLabelText.TextMode = MyTextMode.MultiLine; // go to edit mode</pre> <P><BR>Then, when I render the "Text" of my LabelTextBox control, I do the following code.<BR>&nbsp; <pre class="prettyprint">Server.HtmlEncode( memberVariableTextString );</pre> <P>When I do that,&nbsp;<SPAN style="COLOR: red">õ</SPAN> character renders as <SPAN style="COLOR: red">&amp;#245;</SPAN> for both Label and MultiLine text box modes.<BR>Since, <SPAN style="COLOR: red">&amp;#245;</SPAN>&nbsp;is considered "unsafe" by the ASP.NET Request Validation, the web page throws a Request&nbsp;Validation&nbsp;error<BR>stating that potentially dangerous user input is detected.</P> <P mce_keep="true">&nbsp;</P> <P>However, this is one really weird issue.<BR>I have error logging in my application by sending out Error Notification Email.<BR>In my email, the <SPAN style="COLOR: red">õ</SPAN> character renders out exactly the way it is.</P> <P mce_keep="true">&nbsp;</P> <P>So it has to do something with the Encoding on my ASPX web page.<BR>I am using IIS 6.0 with SQL Server 2000 database with both in U.S. language and locale settings.</P> <P mce_keep="true">&nbsp;</P> <P>Anyone has any clue?<BR>I have seen some articles after some googling on the net to do the following</P><pre class="prettyprint">Response.CharSet = "UTF-8";</pre> <p>&nbsp;I may or may not have found an explanation dealing with this.<br> For those interested in this, please visit the KB URL from Microsoft below.<br> <a class="" title="Microsoft KB 893663" href="http://support.microsoft.com/kb/893663" target="_blank">http://support.microsoft.com/kb/893663</a><br> It deals with &lt;globalization&gt; Element in Web.config file.<br> <br> <br> I will try to give an update on this issue so anyone interested can probably learn from it.<br> </p> <p>Soe</p> 2007-04-10T15:24:29-04:001659379http://forums.asp.net/p/1096884/1659379.aspx/1?Re+%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem) <p>The cause of the problem is this.</p> <pre class="prettyprint">// this renders õ correctly myLabelTextBox.Text = myBusinessObject.DatabaseColumnValue; // But my code is like this // This converts my õ into &amp;#245; myLabelTextBox.Text = Server.HtmlEncode( myBusinessObject.DatabaseColumnValue );</pre> <p>&nbsp;According to the <strong>MSDN Documentation</strong> for Server.HtmlEncode,<br> the method encodes with the following rules. <em>(Note: DBCS means Double Byte Character Set)</em></p> <fieldset style="border-right:lightblue 1px solid; border-top:lightblue 1px solid; margin-left:20px; border-left:lightblue 1px solid; border-bottom:lightblue 1px solid"> <legend style="font-weight:700; color:brown">Quote from MSDN ( <a title="MSDN" href="http://msdn2.microsoft.com/en-us/library/ms525347.aspx"> http://msdn2.microsoft.com/en-us/library/ms525347.aspx</a> ) </legend>If the string to be encoded is DBCS, HTMLEncode converts characters as follows: <ul> <li>All extended characters are converted. </li><li>Any ASCII code character whose code is greater-than or equal to 0x80 is converted to , where &lt;number&gt; is the ASCII character value. </li><li>Half-width Katakana characters in the Japanese code page are not converted.</li></ul> </fieldset> <p>&nbsp;</p> <p>But I really NEED to use Server.HtmlEncode( myString ).<br> Is there a way still how to make õ renders as it is?</p> <p>&nbsp;</p> <p>Thanks,<br> Soe</p> 2007-04-10T16:28:43-04:001659392http://forums.asp.net/p/1096884/1659392.aspx/1?Re+%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem) Nevermind....I just found the problem.<br> I was calling Server.HtmlEncode( myString ) two times and that is the problem.<br> 2007-04-10T16:37:43-04:001659402http://forums.asp.net/p/1096884/1659402.aspx/1?Re+%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem) <p>That's a pretty good idea for a web control.&nbsp; I'm not sure why the label isn't rendering correctly.&nbsp; When you render the text of your control, you would probably want to check which mode it is in, so you know if the text is going to a textbox or a label.&nbsp; If it is going to be in a textbox on the form, have it skip the HtmlEncode.&nbsp; </p> <p>When the control is in ReadOnly mode, how are you having the text displayed.&nbsp; Are you creating an ASP Label, a textbox set to readonly, or are you just having it write out the text?&nbsp; Also, does your control inherit from TextBox, or any other controls?</p> <p>If you could post the Render method of your control, that may help out.&nbsp; It also may help if you could post the HTML that is generated by the control when it is in ReadOnly mode, so we can see exactly how it is being sent to the web browser.&nbsp; When you are viewing the page in IE, go to View Source, and post that.</p> <p>&nbsp;Ben</p> 2007-04-10T16:40:48-04:001659599http://forums.asp.net/p/1096884/1659599.aspx/1?Re+%C3%B5+vs+%C3%B5+Character+Encoding+Server+HtmlEncode+problem+Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem) <blockquote><span class="icon-blockquote"></span> <h4>Benners_J</h4> That's a pretty good idea for a web control. I'm not sure why the label isn't rendering correctly.</blockquote> My control has a Boolean flag called <b>HtmlEncodeLabelMode</b>.<br> When it is true, it will call HttpUtility.HtmlEncode( Text ) on render when it is in Read-Only (or) <b>Label</b> mode. <br> <br> <br> <blockquote><span class="icon-blockquote"></span> <h4>Benners_J</h4> When you render the text of your control, you would probably want to check which mode it is in,<br> so you know if the text is going to a textbox or a label.<br> <br> If it is going to be in a textbox on the form, have it skip the HtmlEncode. </blockquote> For the Textbox <b>Multiline</b> mode, I automatically HtmlEncode exactly like<br> the original behavior of ASP.NET TextBox control.<br> <p>For the Textbox <b>SingleLine</b> mode, I let the ASP.NET TextBox control's methods take care of rendering.</p> <br> <br> <blockquote><span class="icon-blockquote"></span> <h4>Benners_J</h4> When the control is in ReadOnly mode, how are you having the text displayed.<br> <br> Are you creating an ASP Label, a textbox set to readonly, or<br> are you just having it write out the text? </blockquote> For the Read-Only (or) <b>Label</b> mode, I just do either one depending on the <b> HtmlEncodeLabelMode</b> flag. <ul> <li>htmlTextWriter.Write( Text ) </li><li>htmlTextWriter.Write( HttpUtility.HtmlEncode( Text ) ) </li></ul> <br> <br> <blockquote><span class="icon-blockquote"></span> <h4>Benners_J</h4> Also, does your control inherit from TextBox, or any other controls? </blockquote> Yes it is inherited from <b>ASP.NET TextBox control</b>.<br> My project has its own <b>&quot;TextMode&quot; enum</b> to define <b>&quot;Label&quot;</b><br> in addition to SingleLine, Multiline, and Password. 2007-04-10T18:42:05-04:00