Home > .NET, asp.net, c#, html, web user control > How to insert blank rule (<br />) from C# code

How to insert blank rule (<br />) from C# code

August 13th, 2009

Once in a while I have to develop custom user controls that require blank rules in them. Almost always I think again: ‘I should have blogged about it, would have saved me lots of time searching’ How to insert blank rule (<br />) from C# code icon smile … Well, no more, because here it is…

Many people using blank rules in C# code, use the following commands:

var brControl1 = new HtmlGenericControl { InnerHtml = "<br />" };
var brControl2 = new HtmlGenericControl("br");

However, brControl1 also prints a <span> tag and brControl2 prints <br></br>. Both options are not really useful. You should take notice of the fact that the HtmlGenericControl is not meant to be used for anything else than span, body, div and font (what is designed for ‘as the word goes’).  The way to go when printing a blank rule is:

var brControl3 = new LiteralControl("<br />");

This actually prints exactly what you would like it to print, namely <br />. Hope this helps you too How to insert blank rule (<br />) from C# code icon smile ! BTW, if you are using the HtmlTextWriter class, you can also use the WriteBreak() function if you like.

If you like this blog post, you can easily share it:
  • How to insert blank rule (<br />) from C# code services sprite
  • How to insert blank rule (<br />) from C# code services sprite
  • How to insert blank rule (<br />) from C# code services sprite
  • How to insert blank rule (<br />) from C# code services sprite
  • How to insert blank rule (<br />) from C# code services sprite
  • How to insert blank rule (<br />) from C# code services sprite
  • How to insert blank rule (<br />) from C# code services sprite
  • How to insert blank rule (<br />) from C# code services sprite
Categories: .NET, asp.net, c#, html, web user control
Comments are closed.