first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

ASP.NET: Flippant Remarks about Server Controls; C# Code

Server Controls in ASP.NET mean to be an improved alternative to ASP include files. These provide the means to "control" where dynamic HTML appears among static HTML. The simplest Server Control is any HTML tag with the runat attribute set to "server." So we can, for example, set the TITLE tag to run at the server like this:

<title id="TitleDefault" runat="server"><title>

where the ID attribute must be set so ASP.NET can handle this element programmatically. My first instict was to look for a HTML TITLE class in the System.Web.UI.HtmlControls or System.Web.UI.WebControls name spaces. But I did not find such a class.

The assumption here is that somebody at Microsoft wanted me to "find" the control and cast it as a "generic" HTML control:

protected System.Web.UI.HtmlControls.HtmlGenericControl GenControl;
GenControl = (HtmlGenericControl)this.FindControl("TitleDefault");
GenControl.InnerHtml = "My Title Stored in a Database";

The flippant implication here is that the HtmlGenericControl class is the catch-all class for controls that are not dragged and dropped on forms. This class allows us to target any pre-existing static HTML and make it dynamic with ASP.NET.

The other major fundamental scenario is creating HTML on the fly and "injecting" it into a page without the use of static HTML. ASP.NET provides the Literal Web Server Control and the PlaceHolder Web Server Control for this purpose. So, flippantly speaking of course, the Literal Control is like SPAN and the PlaceHolder Control is like DIV.

Generally, I use the PlaceHolder control to load ASCX files (which represent the Web User Control):

protected System.Web.UI.WebControls.PlaceHolder PlaceDocSearch;
System.Web.UI.Control ascxFile = this.LoadControl("./ascx/bodySearchHeader.ascx");
PlaceDocSearch.Controls.Add(ascxFile);

I use the lightweight Literal Control when there is the need to generate HTML with an HtmlTextWriter object. When people call ASP.NET "powerful" I assume they are thinking of designs based Literal controls and HtmlTextWriter objects. But my assumption is probably wrong and we should be amazed at how we can drag and drop controls on our web forms.

mod date: 2003-11-10T23:10:38.000Z