Last post Jun 20, 2008 02:16 AM by david wendelken
Jun 19, 2008 08:45 AM|PaulSinnema|LINK
I'm new to this subject. I would like to create a Server Control for my website. Until now I've created Web User Controls. I think a better approach is using Server Controls (although tougher to develop). What I would like to know is, what is the way to
go about this. Should I use the DOM? I know the straightforward way to just use the 'output.write( HtmlTextWriterTag.Table)' etc... to create HTML but that doesn't seem the right way to me. DOM is more flexibel and less error prone. If the DOM is the right
way to go, could someone please guide me to a tutorial on how to f.i. create the Document. I've already tried several approaches but a stuck on that part. In the Code below you can see what I've tried so far. Appreciate any help.
public class CCContainer : WebControl
private HtmlElement CreateElement(HtmlDocument pobjDoc, string pstrTag, string pstrID)
HtmlElement elem = pobjDoc.CreateElement(pstrTag);
if (pstrID.Length > 0)
elem.Id = pstrID;
private HtmlDocument CreateHTML()
HtmlDocument doc = ???? // How do I get this ????
HtmlElement elem = null;
elem = doc.Body.AppendChild(CreateElement(doc, "Table", "Table1"));
elem = elem.AppendChild(CreateElement(doc, "tr", ""));
elem = elem.AppendChild(CreateElement(doc, "td", ""));
elem.InnerHtml = "This is a test test";
protected override void RenderContents(HtmlTextWriter output)
Jun 19, 2008 09:01 AM|awilinsk|LINK
There is no DOM in server side code. All the HTML output should be done via the HtmlTextWriter. In RenderContents you would write something like this:
output.Write("Contents of cell");
Jun 19, 2008 09:13 AM|gbogea|LINK
In your case I would benefit from using the HtmlControls (HtmlTable, HtmlTableRow, HtmlTableCell) creating the tags you want and then adding them to your page. This link will show an example:
This post creates a login control but the way it's code is similiar to what you want, take a look:
Jun 19, 2008 09:15 AM|PaulSinnema|LINK
Thanks for the quick reply. Aha. so all output is created sequentially and RenderEndTag automatically knows how to end a Tag. Ok, I'll give this a try. Thanks for the Link.
Jun 19, 2008 09:24 AM|awilinsk|LINK
You got it. The "RenderTag" methods keep a stack of what tags are open and when RenderEndTag is called it pops one of the begin tags off the stack.
Jun 19, 2008 09:24 AM|PaulSinnema|LINK
Wow, you guys are fast. Thanks for standing by.
Looks even more promising.
Jun 19, 2008 10:40 AM|david wendelken|LINK
I'll pull out some sample code for you to look at from a few server controls I wrote at home. May take a day or two, depends on spare time and whether I've got electrical power and/or phones and/or internet connectivity at home.
Learning how to write server controls is "a really good thing to do".
Do not expect to figure it out on your own, either via intuition or logic. You won't.
Plan on buying every book with a sizeable section on the topic, plus reading dozens of semi-conflicting articles on the web, and then cursing MS for not providing decent documentation. But when all the cursing is done and you've finally figured it out,
it's really sweet.
Jun 20, 2008 12:40 AM|PaulSinnema|LINK
Thanks in advance. I'm curious about your examples.
Jun 20, 2008 02:16 AM|david wendelken|LINK
I don't pretend to be an expert in custom server controls. I've built a few really neat controls, but it's entirely possible that there are much better ways to do it than I did. When I found a way that worked correctly, I quit looking. [<:o)]
This one is a simple one that overrides an existing control, Compare Validator.. Key differences in behavior are:
severityIcon.ImageUrl = Page.ClientScript.GetWebResourceUrl(