Category Archives: web user control

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

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’ :) … 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 :) ! BTW, if you are using the HtmlTextWriter class, you can also use the WriteBreak() function if you like.