10 short ASP.NET WebForms questions for intermediate level

Here is the list of ten ASP.NET WebForms questions for intermediate level.

1. In which event ASP.NET WebForms controls are fully loaded?

Mainly all controls are accessible in Page_Init event except “ViewState” because it doesn’t fully loaded during this event. The Page_Load event guarantees that all the controls are fully loaded and ready to use.

2. How can we identify that the Page is Post Back?

The IsPostBack property of “Page” object is used to identify that the Page is post pack (rendered in response to HTTP POST request). Here is the example:

protected void Page_Load(object sender, EventArgs e)
    if (IsPostBack)
        //Page is Post Back
        //Page is Not Post Back

3. What is ViewState in ASP.NET WebForms? Where is the ViewState information stored?

The ViewState is a feature that is used by ASP.NET WebForms framework to persist the page and server control values between round trips or post backs.

The ViewState information is stored in the HTML hidden fields. It is automatically used by the ASP.NET framework to persist information that must be preserved between post backs. You can also use ViewState to store application data that is specific to the page, like this:

//Store the values to ViewState
ViewState["testVS"] = txtID.Text;

//Get the value from ViewState
if (ViewState["testVS"] != null)
    txtID.Text = ViewState["testVS"].ToString();

4. What is the lifespan for items stored in ViewState?

Items stored in ViewState live until the current page is not expired. When user requests another page, the previous page data is no longer available. In short, we can say that the lifespan for items stored in ViewState exists till your current page exists.

5. Which is the parent class used for the web server control?

The System.Web.UI.Control class is used as parent for all web server controls.

6. How can you assign page specific attributes in an ASP.NET Application?

The @Page directive is responsible to assign page specific attributes for the page parser and the compiler in an ASP.NET WebForms application. Following is the simple example of @Page directive, which is the first line of your .aspx web page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Default" %>

7. What does the EnableViewState property do? Why do we want it to On or Off?

The EnableViewState property enables the “ViewState” property on the page. When the EnableViewState property is set to false (means Off), the page does not store the users input during post back requests of the web page. On other hand, when this property is set to true (means On), the page saves the users input between post back requests of the webpage. Following is the simple example that states that the EnableViewState property is set to “Off”.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Default" EnableViewState="true" %>

8. What is the Code-behind feature in ASP.NET WebForms?

The Code-behind feature enables you to divide an ASP.NET page into two files – one consisting of the presentation data (which contains the user interface elements, such as HTML and Web Server Controls), and the second one consisting of the business logic (which contains the event-handling process to handle the events that are fired by the controls). It also allows us to debug and trace the code.

The file that contains the presentation data has the “.aspx” extension, for example Home.aspx; whereas the file contains the business logic has the “.cs” or “.vb” extension depending upon the type of selected programming language that is C# or VB respectively, for example “Home.aspx.cs” or “Home.aspx.vb”.

9. What are common web form events available in ASP.NET WebForms?

Following are the common ASP.NET web form events available:

  • Page_Init
  • Page_Load
  • Page_PreRender
  • Page_Unload
  • Page_Disposed
  • Page_Error

10. What is a master page?

A master page is a template for one or more Web Forms. The master page defines how the page will be laid out when presented to the user, with placeholders for content. The MasterPageFile Page Directive in a content page's header is one way to assign a master page. The content pages rely solely on content and leave layout to the master page. ASP.NET merges the content with the master page layout when the content page is requested by a user.