XhtmlTextWriter writes table and attributes problem

H

Hardy Wang

Hi all,
I am building a Composite control, and I need to render Xhtml content for my
control. Here is my sample code:

protected override void Render(HtmlTextWriter output) {
XhtmlTextWriter xWriter = new XhtmlTextWriter(output);

xWriter.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
xWriter.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
xWriter.RenderBeginTag(HtmlTextWriterTag.Table);
xWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
xWriter.RenderBeginTag(HtmlTextWriterTag.Td);
xWriter.Write("Hello world");
xWriter.RenderEndTag();
xWriter.RenderEndTag();
xWriter.RenderEndTag();

xWriter.AddAttribute("src", "logo.gif");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
}

And here is the output

<table>
<tr>
<td>Hello world</td>
</tr>
</table><img src="logo.gif" />

Why attributes are not added to my table tag, but OK for img tag?

Do I miss something?
 
H

Hardy Wang

It seems that it is almost impossible to write "0" as attribute value im many
cases.

For example:

xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddAttribute(HtmlTextWriterAttribute.Border, "0");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.

xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0px");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.

xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddAttribute("borderwidth", "0");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.

xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.

Is it a bug from MS?
 
C

Coskun SUNALI [MVP]

Hi,

Can you try the following code please.

xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddAttribute(HtmlTextWriterAttribute.Border, "0");
xWriter.RenderEndTag();

xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0px");
xWriter.RenderEndTag();

xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddAttribute("borderwidth", "0");
xWriter.RenderEndTag();

xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
xWriter.RenderEndTag();


xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddAttribute(HtmlTextWriterAttribute.Border, "0");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.
xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0px");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.
xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddAttribute("borderwidth", "0");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.
xWriter.AddAttribute(HtmlTextWriterAttribute.Src, "a.gif");
xWriter.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
xWriter.RenderBeginTag(HtmlTextWriterTag.Img);
xWriter.RenderEndTag();
Won't add border="0" to img tag.



--

All the best,
Coskun SUNALI
Microsoft MVP
http://sunali.com
http://propeople.dk
 

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,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top