ASP.NET Interview Questions and Answers (1544) - Page 42

What are the ways to manage state of a page?

There are mainly two ways to maintain state of a page.
1) client side state management
2) server side state management

Client side state management:

a) View state : Temprorary storage of controls value of a page during postbacks.

b) Control state: Control state property allows to persist property information of a control, it can not be off as view state can be off.

c) Hidden fields: You can also store values in hidden fields, hidden fields are not visible to browser, it is rendered as HTML hidden field.

d) Cookies: Store values in a text file or in memory client browser's session. It stores small amount of data.

e) Query strings: Using query string send values one page to another with URL.

Server side state management

a) Application state: You can also store values in application state, it is accessible from any page in web application, Application state persists data through out the application, you can store application specific information in it. Data stores in application persists for all users.

b) Session state: It is similar to application state but the difference is it store value for user specific or browser specific. You can store any value or data structure in session. Session data lost when user's session expired.

c) Profile properties It stores user specific data, It is similar to session state but profile data persist even user session expired.
What is the limitation of query string?

Limitations:
1) Security Issue due to visibility of query string values in browsers address bar.
2) There is a limitation of URL length up to 255 bytes.
3) Only strings are allowed in query string, can not store structured data.
What is viewstate and use of view state?

Viewstate is a client side state management technique or method. View state is a temprory storage media to store any value in page level only.

Viewstate is also used to maintain state of server controls like textbox during postbacks of a page.

In view state, you can also store structured data like arraylist, dataset etc.

Example:

Lets say there is data table filled with data.

DataTable dt=new Datatable();

//fill dt with data and store it in viewstate to persist its value in page during postbacks.

//store value in view state
ViewState["MyTable"]=dt;

//get value from view state
DataTable dt=(DataTable)ViewState["MyTable"];
string strUser=string)

how to disable a session state?

Disable a session state for those pages whoich are not require session state, then disable session on that perticular pages using following line.

<%@ Page EnableSessionState="False" %>

Can be disable session state for whole application if yes then how?

Yes, we can disable session state for whole application by setting in web.config file

<configuration>

<system.web>
<pages enableSessionState="false" />
</system.web>
</configuration>


But if you want to enable session state in perticular pages you can set enableSessionState="true" in page directives.

<%@ Page EnableSessionState="true" %>

How to make session state readonly for a application?

Using following lines we can make session state read only for an application.

<configuration>

<system.web>
<pages enableSessionState="ReadOnly" />
</system.web>
</configuration>

How to disable view state on a page?

Set EnableViewState="false" in page directives.

Example:

<%@ Page EnableViewState="false" %>

What are the option to store a session or what are modes of a session state?

Session state has several storage options, Session state modes are

1) InProc: It stores session state in memory of a web server. This is by default set in web.config.

2)StateServer: It stores session state in seperate process, means ASP.net state service.

3)SqlServer: session state stores in SQL server database.

4)Custome: Store session state in custom storage provider.

5)Off: By setting mode="Off", it disables the session state.
Can we trace view state information?

Yes we can trace view state information by setting trace attribute to true in page directive.

<%@ Page Language="C#" AutoEventWireUp="true" Trace="True"  %>

can we declare private class in namespace?

No we can declare private class in namespace, it will give compile time error.

Example:

namespace example
{
private class ex //error
{
}
}

"Error: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal"
How to load xml into dataset?

Using following lines of code, we can read xml file and load it into data set.

DataSet objDS = new DataSet();

objDS.ReadXml("myfile.xml", XmlReadMode.ReadSchema);

How will you identify whether the page is post backed or not?

using "IsPostBack" property.

Example

protected void Page_Load(object sender, EventArgs e)

{
if (!Page.IsPostBack)
{
//code that runs first time not execute when page is post back.
}
}

When the data in ViewState gets expired?

Data stored on ViewState live till current page does not gets expired. Once page is expired, data in ViewState will be lost.
What is fragment caching?

Fragment Caching provides to cache a specific portion of the page, not the whole page. Fragment Caching is achieved by implementing user controls. User controls then implemeted on pages and cache each user controls individually.

Example:


MyUserControl.ascx
<%@ OutputCache Duration='100' VaryByParam='none' %>

Can we write return statement in finally block?

No, we can not write, compiler will give error, because finally block has to be run in any case whether exception occurrs or not, so can not leave the body of finally block.

try

{
}
catch
{
}
finally
{
return;//compile time error: Control cannot leave the body of a finally clause
}
what is JsRender?

JsRender is a JavaScript library that allows you to define a structure once and reuse it to generate HTML dynamically.

JsRender brings a new templating library to HTML5 development that has codeless tag syntax and high performance, has no dependency on jQuery nor on the Document Object Model (DOM), supports creating custom functions and uses pure string-based rendering.
Can you change a Master Page dynamically at runtime ?

Yes.
To change a master page during run time, you have to set the MasterPageFile property to point to the .master page during the PreInit page event.
How do you apply Themes to an entire application ?

By specifying the theme in the web.config file.

Example:

<configuration>

<system.web>
<pages theme=”BlueMoon” />
</system.web>
</configuration>

How do you exclude an ASP.NET page from using Themes ?

To remove themes from your page, use the EnableTheming attribute of the Page directive.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories