Default Server Controls In an ASP.NET Web Form


In this article, we are going to find out the names of 5 server controls which are present in a WebForm.
In ASP.Net, a WebForm is composed of two different pages. One is code behind (. aspx.cs or .aspx.vb) and the other is designer page (.aspx). Whenever we create a new web application or web site, it has a default webpage, which contains 5 server controls. Those controls are always present even we add new WebForms.
We can verify this by just writing the below lines of code in the Page_Load Event, and running the application.
  1. protected void Page_Load (object sender, EventArgs e)  
  2. {  
  3.    Response.Write("total server control: " + Controls.Count);  
  4. }  
But when you look at the designer page of a Web Form (WebForm1.aspx), it’s very hard to figure it out, what are they?
Look at the below HTML code of a design page.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>    
  2. <! DOCTYPE html>    
  3. <html xmlns="">    
  4. <head runat="server">    
  5.     <title></title>    
  6. </head>    
  7. <body>    
  8.     <form id="form1" runat="server">    
  9.         <div>    
  10.         </div>    
  11.     </form>    
  12. </body>    
  13. </html>     
An ASP.Net web application has two In-Built types of control, one is server and other is html control. We can easily find server control by just looking at the attribute “runat” in a control tag.
In the above HTML code, we can easily spot two server controls, the first is form and the other is head. But wait, where are the others? According to the Count property of Control class, it should be 5.
Are they hidden or generated at runtime or still present in the above HTML code?
Let’s find out their types first by writing the below code in the code behind file (.aspx.cs).
  1. public partial class WebForm1: Page    
  2.    {    
  3. protected void Page_Load (object sender, EventArgs e)    
  4.         {    
  5.              foreach (string controlName in getControlNames())    
  6.             {    
  7.                 Response.Write(controlName + "<br/>");    
  8.             }    
  9.          }    
  10.     private IEnumerable<string> getControlNames()    
  11.         {    
  12.             foreach (Control item in Controls)    
  13.             {    
  14.                 yield return item.GetType().ToString();    
  15.             }       
  16.      }    
  17. }     
By running the application, we will have the below output:
So, the Count property of the Control class is correct. Clearly visible control types from the above result, two controls are HTML type (Head and Form) and the remaining three are Literal type. But when you look at the HTML code one more time very carefully, you will not able to find other controls with “runat” attribute.
Let’s put literal control for comparison with the above HTML code, into the designer page (WebForm1.aspx) from the Toolbox (Under Standard category)
  1. <body>    
  2.     <form id="form1" runat="server">    
  3.         <div>    
  4.              <asp:Literal ID="Literal1" runat="server" Text=""></asp:Literal>    
  6.         </div>     
  7.      </form>    
  8. </body>     
Although in-built literal control (above one) has “runat” attribute, in our case, we are unable to find it.
Let’s dig into more details, by finding out the text content of each control. So, at least we can see the exact place of those controls in HTML code.
To get the text content of Control object in ASP.Net, write the below code:
  1. public partial class WebForm1: Page {  
  2.     protected void Page_Load(object sender, EventArgs e) {  
  3.         StringBuilder textBuilder = new StringBuilder();  
  4.         foreach(Control item in Controls) {  
  5.             using(StringWriter stringWriter = new StringWriter(textBuilder)) {  
  6.                 using(HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter)) {  
  7.                     item.RenderControl(htmlTextWriter);  
  8.                     string text = textBuilder.ToString();  
  9.                 }  
  10.             }  
  11.         }  
  12.     }  
  13. }   
In the above code, the RenderControl method outputs server control content to the provided the HtmlTextWriter object. Then cascades this information to last StringBuilder object. The variable control text holds the full-text representation of each control.
Below is the complete text representation of 5 server controls
  1. Head control: “<head><title></title></head>"
  2. Form control: <form method="post" action="./WebForm1.aspx" id="form1"><div class="aspNetHidden">\r\n<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" /></div><div></div></form>
  3. Literal control: <!DOCTYPE html><html xmlns="\">
  4. Literal control: <body>
  5. Literal control: </body></html>
So, eventually we found those three controls and their location. There are visibly invisible.
In a nutshell, we can state that the ASP.Net compiler converts a readable text or HTML element into a Literal Object which does not require server-side processing.
Thank you so much for reading. I hope you had as much fun reading this as I did writing it.