how to render <br> tag from a user control?

S

Steve Richter

I am using HtmlGenericControl to render html tags like <div> and
<span>.

the problem is the <br> tag does not render correctly when I use this
method. ( it renders as <br></br>, which is seen by the browser as 2
consecutive <br> tags.

public class Html_br : HtmlGenericControl
{
public Html_br()
: base("br")
{
}
}

How can I render the <br> tag so that I can use Controls.Add to add a
<br> tag to the output of my user control?

thanks,

-Steve
 
M

Mark Rae

the problem is the <br> tag does not render correctly when I use this
method. ( it renders as <br></br>, which is seen by the browser as 2
consecutive <br> tags.

How can I render the <br> tag so that I can use Controls.Add to add a
<br> tag to the output of my user control?

<br />
 
S

Steve Richter

I am using HtmlGenericControl to render html tags like <div> and
<span>.

the problem is the <br> tag does not render correctly when I use this
method. ( it renders as <br></br>, which is seen by the browser as 2
consecutive <br> tags.

public class Html_br : HtmlGenericControl
{
public Html_br()
: base("br")
{
}
}

How can I render the <br> tag so that I can use Controls.Add to add a
<br> tag to the output of my user control?

thanks,

-Steve

answered my own question. I use the HtmlGenericControl, but I dont
pass a tag to the constructor and set the InnerHtml to "<br>".

public class Html_br : HtmlGenericControl
{
public Html_br()
: base("br")
{
base.InnerHtml = "<br>";
}
}

still does not seem right, since the documentation of the
HtmlContainerControl, which is the base class of HtmlGenericControl,
says:

"...Serves as the abstract base class for HTML server controls that
map to HTML elements that are required to have an opening and a
closing tag. ..."

<br> does not support the closing tag.

-Steve
 
S

Steve Richter

Yes, it would do... The <br> tag is deprecated these days, and XHTML
compliance needs it to be self-closing.

deprecated? jeez. what was it too easy and unambiguous? ;)

how do I start text on a new line?
<span style="display:block;">line 1</span>
<span style="display:block;">line 2</span>

or has <span> been deprecated also?

thanks,

-Steve
 
M

Mark Rae

deprecated? jeez. what was it too easy and unambiguous? ;)

No - it was non-standards compliant...
how do I start text on a new line?
<span style="display:block;">line 1</span>
<span style="display:block;">line 2</span>

or has <span> been deprecated also?

Have you tried <div>...</div>
 
G

Guest

Hi Steve,

There are few resolutions to the problem:

1. Depending on the browser caps you will get <br> or <br/>
public class HtmlBr : System.Web.UI.HtmlControls.HtmlControl
{
protected override void Render(HtmlTextWriter writer)
{
writer.WriteBreak();
}
}

2. Insetad of deriving simply use LiteralControl

rendring for HTML 4.0
myContainer.Controls.Add(new LiteralControl("<br>"));
or for XHTML 1.0/1.1
myContainer.Controls.Add(new LiteralControl("<br/>"));


Please note you and Mark are both right because you're talking abount HTML
4.01 standard (<br>) and Mark is talking about XHTML <br/> (all tags must be
valid from XML point of view)

Regards

Milosz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top