Best ASP.NET Questions With Answers for Interviews –
Chapter 7: ASP.NET
1. What is the sequence in which ASP.NET events are processed ?
Following is the sequence in which the events occur :
- Control events.
- Page_Unload event.
Page_Init event only occurs when first time the page is started, but Page_Load occurs in subsequent request of the page
2. In which event are the controls fully loaded ?
Page_Load event guarantees that all controls are fully loaded. Controls are also accessed in Page_Init events but you will see that viewstate is not fully loaded during this event
3. How can we identify that the Page is PostBack?
Page object has a “IsPostBack” property which can be checked to know that is the page posted back.
4. How does ASP.NET maintain state in between subsequent request?
Refer caching chapter.
5. What is event bubbling?
Server controls like Datagrid, DataList, Repeater can have other child controls inside them. Example DataGrid can have combo box inside datagrid. These child controls do not raise their events by themselves, rather they pass the event to the container parent (which can be a DataGrid, DataList, Repeater), which passed to the page as “Item Command” event. As the child control send their events to parent this is termed as event bubbling.
How do we assign page specific attributes?
Page attributes are specified using the @Page directive.
6. If we want to make sure that no one has tampered with ViewState, how do we ensure it?
Set the @Page directive EnableViewStateMac to True.
7. What is the use of @Register directives?
@Register directive informs the compiler of any custom server control added to the page.
8. What is SmartNavigation property?
It’s a feature provided by ASP.NET to prevent flickering and redrawing when the page is posted back. It is supported by Internet Explorer only.
10. What is AppSetting Section in “Web.Config” file ?
Web.config file defines configuration for a webproject. Using “AppSetting” section we can define user defined values. For Example we can define “ConnectionString” section which will be used throughout the project for database connection.
<add key=”ConnectionString” value=”server=xyz;pwd=www;database=testing” />
11. Where is ViewState information stored?
In HTML Hidden Fields.
12. What is the use of @OutputCache directive in ASP.NET?
It is basically used for caching.
13. How can we create custom controls in ASP.NET ?
User controls in ASP.NET are created using .ASCX files. After .ASCX file is created you need to two things in order that the ASCX can be used in project:
- Register the ASCX control in page using the <%@ Register directive. Example <%@ Register tagprefix=”Accounting” Tagname=”footer” Src=”Footer.ascx” %>
- Now to use the above accounting footer in page you can use the below directive. <Accounting:footer runat=”server” />
14. What types of validation controls are provided by ASP.NET?
There are six main types of validation controls:
- RequiredFieldValidator – It checks whether the control have any value. It’s used when you want the control should not be empty.
- RangeValidator – It checks if the value in validated control is in that specific range. Example txtCustomerCode should not be more than eight length.
- CompareValidator – It checks that the value in controls should match some specific value. Example Textbox TxtPi should be equal to 3.14.
- RegularExpressionValidator – When we want the control value should match with a specific regular expression.
- CustomValidator – It is used to define user-defined validation.
- ValidationSummary – It displays summary of all current validation errors.
15. What is “AutoPostBack” feature in ASP.NET ?
If we want the control to automatically postback in case of any event, we will need to check this attribute as true. Example on a ComboBox change we need to send the event immediately to the server side then set the “AutoPostBack ” attribute to true.
16. How can you enable automatic paging in DataGrid?
In order to enable paging in Datagrid you need to (very short version):
- Set the “AllowPaging” property to true.
- In PageIndexChanged event handler set the current PageIndex clicked.
Below is an example in C#:
protected System.Web.UI.WebControls.DataGrid DataGrid1;
//Binding DataGrid with a Data Source (DataSet in our Case)
DataGrid1.DataSource = dataSet1;
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.GridPageIndexChanged);
//Implement the EventHandler
private void GridPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
DataGrid1.CurrentPageIndex = e.NewPageIndex;
//Bind the DataGrid again with the Data Source
DataGrid1.DataSource = dataSet1;
17. What is the use of “GLOBAL.ASAX” file ?
It allows to handle ASP.NET application level events and setting application-level variables.
18. What is the difference between “Web.config” and “Machine.Config” ?
“Web.config” files apply settings to each web application, while “Machine.config” file apply settings to all ASP.NET applications.
19. What are SESSION and APPLICATION objects?
In short, Session object stores information between HTTP requests for a particular user, while Application object is global across users.
20. What is the difference between Server.Transfer and Response.Redirect ?
Following are the major differences between them:
- Response.Redirect sends message to the browser saying it to move to some different page, while Server.Transfer does not send any message to the browser but rather redirects the user directly from the server itself. So in Server.Transfer
there is no round trip while Response.Redirect has a round trip and hence puts a load on server.
- Using Server.Transfer you cannot redirect to a different from the server itself. This cross server redirect is possible only using Response.Redirect.
- With Server.Transfer you can preserve your information. It has a parameter called as “preserveForm”. So the existing query string etc. will be able in the calling page.
Please submit your queries