views:

89

answers:

2

i use such code, but it renders with error <li class="dd0"><div id="dt1"<a href="http://localhost:1675/Category/29-books.aspx"&gt;Books&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;

there is no > in opening tag div. what the problem?

writer.WriteBeginTag("li");
                //writer.WriteAttribute("class", this.CssClass);
                writer.WriteAttribute("class", "dd0");
                if (!String.IsNullOrEmpty(this.LiLeftMargin))
                {
                    writer.WriteAttribute("style", string.Format("margin-left: {0}px", this.LiLeftMargin));
                }
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.WriteBeginTag("div");
                writer.WriteAttribute("id", "dt1");
                this.HyperLink.RenderControl(writer);
                writer.WriteEndTag("div");
                writer.WriteEndTag("li");
+4  A: 

You need to add a call to writer.Write(HtmlTextWriter.TagRightChar) after you have written your attributes (like you have already done for the li element:

writer.WriteBeginTag("div");
writer.WriteAttribute("id", "dt1");
writer.Write(HtmlTextWriter.TagRightChar);

The MSDN docs for WriteBeginTag explicitly state this behaviour:

The WriteBeginTag method does not write the closing angle bracket (>) of the markup element's opening tag. This allows the writing of markup attributes to the opening tag of the element. Use the TagRightChar constant to close the opening tag when calling the WriteBeginTag method.

adrianbanks
Just for reference (as it doesn't apply in your case), you can use `WriteFullBeginTag` (http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.writefullbegintag(v=VS.90).aspx) to output the closing `<` if you don't need to output any attributes on the element.
adrianbanks
+1  A: 

You dont need to use writer.Write(HtmlTextWriter.TagRightChar);, the writer is intelligent enough to close the tags. Again when using WriteRenderEndTag, you dont need to supply a parameter.

Edit. Im talking about different methods here. Here is the code I would use:

    output.AddAttribute("class", "dd0");

    if (!String.IsNullOrEmpty(this.LiLeftMargin))
    {
        output.AddAttribute("style", string.Format("margin-left: {0}px", LiLeftMargin));
    }

    output.RenderBeginTag("li");

    //output.Write(HtmlTextoutput.TagRightChar);
    output.AddAttribute("id", "dt1");
    output.RenderBeginTag("div");

    this.HyperLink.RenderControl(output);

    output.RenderEndTag(); //div
    output.RenderEndTag(); //li
James Westgate
no, it not work. WriteEndTag require parameter
kusanagi