Hi everybody, this is a problem regarding Character Encoding and
Server.HtmlEncode.
I hope someone can help me out here.
This is the situation. 1) The user enters õ into a TextBox control and my application saves it into the Database without any changes. 2) The user comes back and see "Read Only" version of what he entered. I do this which results in the encoded
õ on the web page.
Displays õ in place of the label just as it should. There may be something else contributing to the result you're getting for that, such as another tag or css attribute.
2) Textboxes don't need their values to be HtmlEncoded. They display and return the unencoded string. The following line should work fine.
The problem is that I am actually using my own Custom Web Control
which can switch between "Text Input" mode and "Label" mode.
I have the control behave like that so I don't need to keep 2 Controls (1 TextBox and 1 Label) on the web page
to switch between "Read Only" and "Edit" modes.
I just do either one of the following lines of code.
myLabelText.TextMode = MyTextMode.Label; // go to read-only mode
myLabelText.TextMode = MyTextMode.MultiLine; // go to edit mode
Then, when I render the "Text" of my LabelTextBox control, I do the following code.
Server.HtmlEncode( memberVariableTextString );
When I do that, õ character renders as õ for both Label and MultiLine text box modes. Since, õ is considered "unsafe" by the ASP.NET Request Validation, the web page throws a Request Validation error stating that potentially dangerous user input is detected.
However, this is one really weird issue. I have error logging in my application by sending out Error Notification Email. In my email, the õ character renders out exactly the way it is.
So it has to do something with the Encoding on my ASPX web page. I am using IIS 6.0 with SQL Server 2000 database with both in U.S. language and locale settings.
Anyone has any clue? I have seen some articles after some googling on the net to do the following
Response.CharSet = "UTF-8";
I may or may not have found an explanation dealing with this.
For those interested in this, please visit the KB URL from Microsoft below. http://support.microsoft.com/kb/893663
It deals with <globalization> Element in Web.config file.
I will try to give an update on this issue so anyone interested can probably learn from it.
// this renders õ correctly
myLabelTextBox.Text = myBusinessObject.DatabaseColumnValue;
// But my code is like this
// This converts my õ into õ
myLabelTextBox.Text = Server.HtmlEncode( myBusinessObject.DatabaseColumnValue );
According to the MSDN Documentation for Server.HtmlEncode,
the method encodes with the following rules. (Note: DBCS means Double Byte Character Set)
But I really NEED to use Server.HtmlEncode( myString ).
Is there a way still how to make õ renders as it is?
That's a pretty good idea for a web control. I'm not sure why the label isn't rendering correctly. 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.
If it is going to be in a textbox on the form, have it skip the HtmlEncode.
When the control is in ReadOnly mode, how are you having the text displayed. Are you creating an ASP Label, a textbox set to readonly, or are you just having it write out the text? Also, does your control inherit from TextBox, or any other controls?
If you could post the Render method of your control, that may help out. 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. When you are
viewing the page in IE, go to View Source, and post that.
That's a pretty good idea for a web control. I'm not sure why the label isn't rendering correctly.
My control has a Boolean flag called HtmlEncodeLabelMode.
When it is true, it will call HttpUtility.HtmlEncode( Text ) on render when it is in Read-Only (or)
Label mode.
Benners_J
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.
If it is going to be in a textbox on the form, have it skip the HtmlEncode.
For the Textbox Multiline mode, I automatically HtmlEncode exactly like
the original behavior of ASP.NET TextBox control.
For the Textbox SingleLine mode, I let the ASP.NET TextBox control's methods take care of rendering.
Benners_J
When the control is in ReadOnly mode, how are you having the text displayed.
Are you creating an ASP Label, a textbox set to readonly, or
are you just having it write out the text?
For the Read-Only (or) Label mode, I just do either one depending on the
HtmlEncodeLabelMode flag.
htmlTextWriter.Write( Text )
htmlTextWriter.Write( HttpUtility.HtmlEncode( Text ) )
Benners_J
Also, does your control inherit from TextBox, or any other controls?
Yes it is inherited from ASP.NET TextBox control.
My project has its own "TextMode" enum to define "Label"
in addition to SingleLine, Multiline, and Password.
soelinn
Member
102 Points
36 Posts
õ vs õ ( Character Encoding + Server.HtmlEncode problem)
Apr 09, 2007 10:23 PM|LINK
Hi everybody, this is a problem regarding Character Encoding and Server.HtmlEncode.
I hope someone can help me out here.
This is the situation.
1) The user enters õ into a TextBox control and my application saves it into the Database without any changes.
2) The user comes back and see "Read Only" version of what he entered. I do this which results in the encoded õ on the web page.
3) On top of that, when I go back into "Edit" mode, I do the following and have the same result as #2 above.
Any ideas how I can fix this?
I would really appreciate any help.
Thanks in advance,
Soe
Benners_J
Contributor
2857 Points
442 Posts
Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem)
Apr 09, 2007 11:00 PM|LINK
1) The line
Label1.Text = Server.HtmlEncode("õ")
Displays õ in place of the label just as it should. There may be something else contributing to the result you're getting for that, such as another tag or css attribute.
2) Textboxes don't need their values to be HtmlEncoded. They display and return the unencoded string. The following line should work fine.
myTextBoxControl.Text = databaseStoredColumnValue;
soelinn
Member
102 Points
36 Posts
Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem)
Apr 10, 2007 03:24 PM|LINK
The problem is that I am actually using my own Custom Web Control
which can switch between "Text Input" mode and "Label" mode.
I have the control behave like that so I don't need to keep 2 Controls (1 TextBox and 1 Label) on the web page
to switch between "Read Only" and "Edit" modes.
I just do either one of the following lines of code.
Then, when I render the "Text" of my LabelTextBox control, I do the following code.
When I do that, õ character renders as õ for both Label and MultiLine text box modes.
Since, õ is considered "unsafe" by the ASP.NET Request Validation, the web page throws a Request Validation error
stating that potentially dangerous user input is detected.
However, this is one really weird issue.
I have error logging in my application by sending out Error Notification Email.
In my email, the õ character renders out exactly the way it is.
So it has to do something with the Encoding on my ASPX web page.
I am using IIS 6.0 with SQL Server 2000 database with both in U.S. language and locale settings.
Anyone has any clue?
I have seen some articles after some googling on the net to do the following
I may or may not have found an explanation dealing with this.
For those interested in this, please visit the KB URL from Microsoft below.
http://support.microsoft.com/kb/893663
It deals with <globalization> Element in Web.config file.
I will try to give an update on this issue so anyone interested can probably learn from it.
Soe
soelinn
Member
102 Points
36 Posts
Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem)
Apr 10, 2007 04:28 PM|LINK
The cause of the problem is this.
According to the MSDN Documentation for Server.HtmlEncode,
the method encodes with the following rules. (Note: DBCS means Double Byte Character Set)
But I really NEED to use Server.HtmlEncode( myString ).
Is there a way still how to make õ renders as it is?
Thanks,
Soe
soelinn
Member
102 Points
36 Posts
Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem)
Apr 10, 2007 04:37 PM|LINK
I was calling Server.HtmlEncode( myString ) two times and that is the problem.
Benners_J
Contributor
2857 Points
442 Posts
Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem)
Apr 10, 2007 04:40 PM|LINK
That's a pretty good idea for a web control. I'm not sure why the label isn't rendering correctly. 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. If it is going to be in a textbox on the form, have it skip the HtmlEncode.
When the control is in ReadOnly mode, how are you having the text displayed. Are you creating an ASP Label, a textbox set to readonly, or are you just having it write out the text? Also, does your control inherit from TextBox, or any other controls?
If you could post the Render method of your control, that may help out. 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. When you are viewing the page in IE, go to View Source, and post that.
Ben
soelinn
Member
102 Points
36 Posts
Re: õ vs õ ( Character Encoding + Server.HtmlEncode problem)
Apr 10, 2007 06:42 PM|LINK
When it is true, it will call HttpUtility.HtmlEncode( Text ) on render when it is in Read-Only (or) Label mode.
For the Textbox Multiline mode, I automatically HtmlEncode exactly like
the original behavior of ASP.NET TextBox control.
For the Textbox SingleLine mode, I let the ASP.NET TextBox control's methods take care of rendering.
For the Read-Only (or) Label mode, I just do either one depending on the HtmlEncodeLabelMode flag.
Yes it is inherited from ASP.NET TextBox control.
My project has its own "TextMode" enum to define "Label"
in addition to SingleLine, Multiline, and Password.