ASP.NET Custom Server Controls: Breaking the Secrets of Rendering - Secrets of “Control” class rendering
(Page 2 of 5 )
The “Control” class mainly concentrates on the following methods to render:
- RenderControl
- Render
- RenderChildren
Actually, any beginner would know only the “Render” method, but not the rest! Breaking the secret, actually, the ASP.NET page framework first executes the “RenderControl” method. This method in turn calls the “Render” method. Finally the “Render” method calls “RenderChildren.” In fact, the ASP.NET page framework would never call the “Render” or “RenderChildren” methods directly!
Let us further break into the secrets of their implementation internally. Internally within the ASP.NET framework, the “RenderControl” has been implemented something like this:
Public void RenderControl(HtmlTextWriter Writer)
{
If (visible)
Render (Writer);
}
Now, I think you grasp some idea. The “Render” method gets executed by the “RenderControl” method if and only if the control has its “visible” property set to “true.” In general, it is unnecessary to work with the above method.
Coming to the “Render” method, the internal implementation would be something like this:
Protected virtual void Render(HtmlTextWriter Writer)
{
RenderChildren(writer);
}
The above code fragment is very clear and I hope that I need not explain much about it. But, if your control doesn’t have any child controls, you need not place “RenderChildren” any more. If you have any child controls, generally that should be placed at the bottom.
Coming to the “RenderChildren” method, the internal implementation would be something like this:
Protected virtual void Render(HtmlTextWriter Writer)
{
Foreach (Control c in Controls)
c.RenderControl(writer);
}
The above method executes the “RenderControl” method of every child control present within our custom control. If this method is overridden by us and if we don’t include the above “for” loop, none of our child controls would get displayed!
Of course, there exists another way to stop the rendering of child controls. The following code fragment ensures that our custom control doesn’t have any child controls to display:
Protected override ControlCollection CreateControlCollection()
{
Return new EmptyControlCollection(this);
}
Next: The Secret behind “Render” method of “WebControl” class >>
More ASP.NET Articles
More By Jagadish Chaterjee