ASP.NET - How To Write a Non-Breaking Space From HTMLTextWriter

Asked By Patrick Skelton on 02-Sep-11 05:24 AM

Does anyone know the best way to write a non-breaking space from an ASP.NET HTMLTextWriter?

Assuming writer is my HTMLTextWriter passed into my overridden Render method, do I just do writer.Write( @"nbsp;" ); or is there a constant defined somewhere, similar to the one that writes the closing right tag - writer.Write( HtmlTextWriter.TagRightChar ); ?

It is no big deal, but I like to do things the 'correct' way if there is one.

I would be very grateful for anyone taking the time to reply to this.

 Kind Regards, Patrick
Jitendra Faye replied to Patrick Skelton on 02-Sep-11 05:38 AM
Try this code-

protected virtual void RenderText(HtmlTextWriter writer, string text)

call this function like this-

 RenderText(writer, " ");
Try this code and let me know.
Patrick Skelton replied to Jitendra Faye on 02-Sep-11 05:42 AM

Thanks for the reply.  That works fine but exactly how the code I have now works.  I obviously wasn't very clear in my question.  I was simply asking if the string " " is defined anywhere in ASP.NET as a constant.

Kind wishes, Patrick

Anoop S replied to Patrick Skelton on 02-Sep-11 06:12 AM
If you want to strip automatically and programmatically the addition of &nbsp entities then you can implement a content filter that will remove these entities when switching to Html mode or submitting the content. Here is a basic example:

<script type="text/javascript">
function RadEditorCustomFilter()
    this.GetHtmlContent = function (content)
re = /&nbsp;/gi  //search for &nbsp; entities in the content
newContent = content.replace(re, "");  //and replace them
      return newContent;
/* Use the editor onload function to register the filter with the FiltersManager */
function onClientLoad (editor)
    var customFilter = new RadEditorCustomFilter();
<radE:RadEditor id="RadEditor1" OnClientLoad="onClientLoad" Runat="server"></radE:RadEditor>
Patrick Skelton replied to Anoop S on 02-Sep-11 07:40 AM
Mmm ... still have a communication problem.  Don't really know how else to write the question.  I am rendering HTML for my own custom control.  When doing this, the HTMLTextWriter class provides a few constants to assist with putting commonly-used strings of characters into the HTML stream, such as:

HtmlTextWriter.SelfClosingChars (which means a space followed by a forward slash)
HtmlTextWriter.TagRightChar (which is a "&gt;" character )

I just wondered if there was constant defined anywhere for writing "&nbsp;" into the HTML stream.

Thanks anyway.

- Patrick

Radhika roy replied to Patrick Skelton on 02-Sep-11 09:47 AM

Use this code-

string Break ="&nbsp;";


Hope this will help you.

Patrick Skelton replied to Radhika roy on 02-Sep-11 09:49 AM
Thank you all for the replies.

- Patrick