asp.net - Using HtmlTextWriter to Render Server Controls? -


I am writing RenderContents () method method of my ASP.NET server control to output content For the use of the HtmlTextWriter object, I am writing for the control using the HtmlTextWriter , such as opening and closing every tag and stopping it In order to add every attribute, many lines will be needed in the end I think that I end up with the code I am going that which is too much time for this.

I was thinking that if I had a seriesal class like StringBuilder , my code would be much more cleaner to read and easier to write.

What was I thinking, is there any reason to use the HtmlTextWriter object in my entire control? Apart from the security check (I'm assuming), it involves making sure that you do not write tags in the wrong order or make invalid markup, I do not see any reason.

It seems like it would be easy to do something like this:

  Protected Override Zero RenderContents (HtmlTextWriter Output) {StringBuilder s = New StringBuilder (); S.Append ("lot") Append ("of") .Append ("string"); Output.BeginRender (); Output.Write (s.ToString ()); Output.EndRender (); }  

Is there a reason, would it be a bad idea?

Update In response to the answer:
I did not think much about the memory requirements of a separate StringBuilder object immediately Was about to make a cover for HtmlTextWriter so that it could be chained so that an additional string was not created.

  Public category ChainedHtmlTextWriter {Private HtmlTextWriter _W; Public ChainedHtmlTextWriter (HtmlTextWriter Writer) {_W = Author; } Type public ChainedHtmlTextWriter & lt; T & gt; (T value) {_W.Write (value); This return; } Public ChainedHtmlTextWriter WriteLine & lt; T & gt; (T value) {_W.WriteLine (value); This return; }}  

I work on an application where developers follow you the terrible path Do the search It tells about the days when you have to type your own ISPI DLL, which is out of HTML code. This is a constant headache to work. If your code is mostly string, then something is wrong.

Most of the code of this type that I change I intend to server object, configure their desired qualities, and then tell them. Render Control (Author). This makes it easy to read and work with code. If anybody is affected by the overhead, then I am ready to accept it (in fact, after my changes have been made, the application usually runs fast, so it is not possible that this is not the case, but I do not know Is my code).

Strings are a simple defect in your material to work hard, when the HTML standard changes, the code I work on was written 04/05, and since then & lt; BR & gt; Has become & lt; Br / & gt; And the uppercase HTML tags are no longer kosher, etc. If they were using server controls, then those server controls have changed their output to HTML without doing anything. This is just a simple example.

EDIT: Oh, and BTW, Bezandender, and Anderender have no implementation. They are placeholders for you and can provide custom functionality for overlapping and in the HtmlTextWriter-derived category.

EDIT2: Sometimes it is difficult for always , such as for containers and accessories. I am doing a lot. Controls.Add () and then provide a container later, sometimes I do this:

  the author. Additional attributes (HtmlTextWriterAttribute.Class, "myContainerClass"); Writer.RenderBeginTag (HtmlTextWriterTag.Div); // do some stuff, reader control on some other controls, etc. Author. RenderAndtag ();  

As mentioned, this will provide the correct HTML, even if the future of a div changes html, so I do not have any stringent coded strings.


Comments