Best Practices for Writing HTML in Web Parts?

Something that's as puzzling as the Cadbury secret (and we all know that us programmers figured that out long ago) is just what's the best way to write out HTML in Web Parts? So call me masochistic but I write Web Parts with code. Yes, it's ugly. Yes, it's painful. Yes, you could use something cool like SmartPart or load the controls yourself (but that brings on a host of other issues like Code Access Security so we won't go into that).

For those of us that hold true to the "old fashioned" way, what's the best way to write all that code out? Consider these two approaches that writes out a label and control in a row for a form:

private void RenderRow(HtmlTextWriter output, Label label, WebControl control)

{

       output.Write("<tr><td class=\"ms-formlabel\" valign=\"top\" nowrap=\"true\">");

       label.RenderControl(output);

       output.Write("</td><td class=\"ms-formbody\">");

       control.RenderControl(output);

       output.Write("</td></tr>");

}

 

private void RenderRow(HtmlTextWriter output, Label label, WebControl control)

{

       output.RenderBeginTag("tr");

       output.RenderBeginTag("td");

       output.AddAttribute("class", "ms-formlabel");

       output.AddAttribute("valign", "top");

       output.AddAttribute("nowrap", "true");

       label.RenderControl(output);

       output.RenderEndTag();

       output.RenderBeginTag("td");

       output.AddAttribute("class", "ms-formbody");

       control.RenderControl(output);

       output.RenderEndTag();

       output.RenderEndTag();

}

 

Both output exactly the same HTML. Does it matter? The first approach is less lines but is it any more (or less) readable? Or maybe everything should be built up in a StringBuilder class and slammed out to the HtmlTextWriter? Or is it simply whatever is readable or maintainable works? Looking for your thoughts, ideas, suggestions, rants, assorted concealed lizards of any kind.

5 Comments

  • In theory, using the second method would create browser-independent HTML. (Of course, using a different browser on a SharePoint site has its own issues...)

  • In a major SharePoint app I worked on last year, I created some classes that I derived from HtmlTable and just called the RenderControl method to generate the HTML.



    Given that the classes exist, I find it obtuse to &quot;manually&quot; write every item to the HtmlTextWriter for the part.



    I even managed to extend the class to handle localization and addition of constituent web controls. Saved TONS of time.

  • And as a side note, with total disregard for the original question: Writing directly to the StreamWriter is preferred over using a StringBuilder. Primarily because writing to a stream is faster than using a string buffer.



    &gt;S

  • I'd have passed the output via ref. :)



    Actually, I prefer the second way above the first. Easier to maintain in the long run. Reason I say that is because it's line by line. I'd probably even add tabs just to keep the appearance. What I hate the most is a bunch of html shoved in a string.

  • Like the article Daniel mentioned, creating controls (and tables) is much more &quot;readable&quot;.



    for example:



    Table mainTable = new Table ();

    this.Controls.Add (mainTable);



    for (int i = 0; i &lt; 2; i++) {

    TableRow row = new TableRow ();

    mainTable.Rows.Add (row);

    row.Cells.Add (new TableCell ());

    }



    this.RenderChildren (writer);



    If you do it the second way, you'll be lost for sure after 6 cells!

Comments have been disabled for this content.