Search code examples
asp.nethtmltextwriter

Alternatives to HtmlTextWriter in a System.Web.UI.Control?


Can you use FluentHTML or other alternatives in an web application user control?

Can you put a markup file in a System.Web.UI.Control like the System.Web.UI.UserControl but without the burden of the Page object?

I would like to reduce the number of lines of the following code:

writer.Write(SacrificalMarkupPage.GenerateControlMarkup(new DialogConfirm()));

writer.AddAttribute("class", string.Format("ajaxResponse {0}", action));
writer.RenderBeginTag("div");

writer.RenderBeginTag("h4");
writer.WriteEncodedText("Delete selected item?");
writer.RenderEndTag(); //h4

var queryString = new HttpQueryString
  {
    {"confirmed", "true"},
    {"src", urlReferer.PathAndQuery}
  };

writer.AddAttribute("href", queryString.MakeQueryString(url).PathAndQuery);
writer.AddAttribute("class", "delete");
writer.RenderBeginTag("a");
writer.WriteEncodedText("Yes");
writer.RenderEndTag(); //a.delete

writer.WriteEncodedText(" | ");

writer.AddAttribute("href", urlReferer.PathAndQuery);
writer.AddAttribute("class", "back");
writer.RenderBeginTag("a");
writer.WriteEncodedText("No");
writer.RenderEndTag(); //a.back

writer.RenderEndTag(); //div.ajaxResponse.ACTION

Thanks


Solution

  • I developed my own helper tools, now i can write markup this way:

            using (new Xhtml.BlockTags.Body(writer))
            {
                using (new Xhtml.BlockTags.Div("layout", string.Empty, true, writer))
                {
                    using (new Xhtml.BlockTags.Div("header", string.Empty, writer))
                    {
                        _header.Render(writer);
                    }
                    using (new Xhtml.BlockTags.Div("content", string.Empty, writer))
                    {
                        _content.Render(writer);
                    }
                    AjaxStatus.Render(writer);
                }
            }
    

    in this example, the first parameter of a blocktag is the ID, the second the class, the last a reference to the writer