Interview Questions ASP.NET Web Forms
Interview Questions ASP.NET Web Forms
1. What is the difference between ASP.NET Web Forms and ASP.NET
MVC?
2. Explain the page life cycle in ASP.NET Web Forms.
3. What is ViewState in ASP.NET Web Forms? How does it work?
4. How can you manage the state of a page in ASP.NET Web Forms?
5. What are the different types of controls available in ASP.NET Web
Forms?
6. How would you implement validation in ASP.NET Web Forms?
7. What is the role of the Global.asax file in Web Forms?
8. How do you handle events in ASP.NET Web Forms?
9. Explain the difference between Server.Transfer and Response.Redirect
in Web Forms.
10. What is the use of the PostBack property in ASP.NET Web Forms?
11. How do you implement Master Pages in Web Forms?
12. Explain the concept of DataBinding in Web Forms and provide an
example.
13. What is the difference between a Literal control and a Label control in
Web Forms?
14. What are HTTP Handlers and HTTP Modules in Web Forms?
15. How would you manage session state in ASP.NET Web Forms?
16. Difference Between Repeater and GridView?
ASP.NET Web Forms
1.What is the difference between ASP.NET Web Forms and ASP.NET MVC?
Ans:
Data Binding Automatic binding with Manual data binding via HTML
controls helpers
Design Less flexible in HTML output Full control over HTML and output
Flexibility
Ans:The ASP.NET Web Forms Page Life Cycle refers to the series of events
that occur when a page is requested and processed. Here’s a brief summary
of each phase:
1. Page Request: The page is requested by the user, and the ASP.NET
engine handles the request.
2. Initialization (Init): Controls are initialized, and their properties are set,
but they are not yet populated with data.
3. Load: Controls are loaded with data, and properties like values and
labels are set. If it's a postback, the controls retain previous values.
4. Postback Event Handling: User events (like button clicks) are handled.
This happens only if it's a postback.
5. Rendering: The page’s HTML output is generated, and the final HTML is
sent to the browser.
6. Unload: Resources are cleaned up after the page has been fully
rendered.
Key Concepts:
How it works:
Key Points:
4.How can you manage the state of a page in ASP.NET Web Forms?
Ans:In ASP.NET Web Forms, you can manage the state of a page using the
following methods:
Ans:In ASP.NET Web Forms, there are several types of controls available,
including:
Ans:In ASP.NET Web Forms, validation can be implemented using Validation Controls.
These controls allow you to validate user input on the client side and server side.
How to Implement:
Example:c# code
{
// Code that runs on application startup
Ans:In ASP.NET Web Forms, events are handled by associating event handlers with
controls, such as buttons or dropdown lists. These events are triggered by user actions
like clicks, selections, or changes.
Example:code
Ans:Server.Transfer and Response.Redirect are both used for page navigation, but they differ in
the following ways:
1. Server.Transfer:
○ Happens on the server side.
○ URL in the browser does not change.
○ No round-trip to the client, making it faster.
○ Can pass data using Context.Items.
2. Response.Redirect:
○ Happens on the client side.
○ URL in the browser changes.
○ Involves a round-trip to the client, making it slower.
○ Data must be passed using query strings, session, or cookies.
Ans:The PostBack property in ASP.NET Web Forms indicates whether the page is being
requested for the first time or is being processed after a form submission (postback).
Example:C# code
if (!IsPostBack)
Ans:In ASP.NET Web Forms, Master Pages are used to provide a consistent layout for
multiple pages in a website.
Example: code
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
2.Create Content Pages:
Example: code
● In the content page, use <asp:Content> tags to add content inside the master
page’s ContentPlaceHolder.
Example: code
<h1>Welcome to My Page!</h1>
</asp:Content>
Result:
The content pages will inherit the layout and elements defined in the master page,
providing a consistent structure across the site.
Example:code
Code-Behind:C# code
{
if (!IsPostBack)
};
GridView1.DataSource = employees;
GridView1.DataBind();
Result: A GridView displays the ID, Name, and Dept columns dynamically.
DataBinding in Web Forms connects UI controls (e.g., GridView, DropDownList) to data sources
(e.g., databases, collections) for dynamic content display. It can be Declarative (using <%# %>
in markup) or Programmatic (using DataBind() in code-behind).
Example:code
if (!IsPostBack)
GridView1.DataSource = new[] {
};
GridView1.DataBind();
Result: The GridView displays rows dynamically with ID, Name, and Dept columns.
To bind data dynamically in Web Forms, you can connect your UI controls to dynamic
data sources such as a database or an API. Here's how you can achieve dynamic
data binding:
html
Copy code
csharp:code
using System;
using System.Data;
using System.Data.SqlClient;
if (!IsPostBack)
BindGridView();
string query = "SELECT ID, Name, Dept FROM Employees"; // Replace with your table and fields
adapter.Fill(dataTable);
GridView1.DataSource = dataTable;
GridView1.DataBind();
}
Explanation
Result
The GridView displays rows dynamically with data fetched from the database. Any
changes to the database (e.g., new rows, updated fields) will reflect in the UI after a
page refresh.
This approach works for other controls like DropDownList or Repeater with similar
methods for setting the DataSource and calling DataBind().
Key Difference: Handlers process specific requests, while modules can globally
affect all requests in the application's lifecycle.
15. How would you manage session state in ASP.NET Web Forms?
Ans:In ASP.NET Web Forms, session state is used to store user-specific data across multiple pages during a
user's session. You can manage session state in several ways:
1. In-Proc (default): Stores session data in memory on the web server. Fast but not suitable for
web farms.
2. State Server: Stores session data on a separate server, enabling session sharing across
multiple web servers.
3. SQL Server: Stores session data in a SQL Server database, allowing persistence and session
sharing across multiple web servers in a web farm.
4. Custom Session State: Allows you to implement a custom storage mechanism for session
data.
xml: code
<system.web>
</system.web>
To use session:
Managing Session Lifetime: You can manage the session timeout and expiration through the
timeout attribute in web.config.
Ans:
HTML Control Full control over HTML structure Limited customization with
predefined layouts
Built-in Features No built-in features (e.g., paging, sorting) Supports paging, sorting,
editing, and deleting
Key Takeaway: