Sie sind auf Seite 1von 10

ASP.

NET Interview Questions


What is STATE MANGEMENT?
State management is implemented in order to retain information about
the user requests. Web pages are stateless. Each request creates new page
without retaining any previous information about the user requests.
There are 2 types State Management:
1. Client – Side State Management
This stores information on the client's computer by embedding the information
into a Web page, a uniform resource locator (url), or a cookie. The techniques
available to store the state information at the client end are listed down below:
A. View State – Asp.Net uses View State to track the values in the
Controls. You can add custom values to the view state. It is used by the
Asp.net page framework to automatically save the values of the page and
of each control just prior to rendering to the page. When the page is
posted, one of the first tasks performed by page processing is to restore
view state.
B. Control State – If you create a custom control that requires view state
to work properly, you should use control state to ensure other developers
don’t break your control by disabling view state.
C. Hidden fields – Like view state, hidden fields store data in an HTML
form without displaying it in the user's browser. The data is available only
when the form is processed.
D. Cookies – Cookies store a value in the user's browser that the browser
sends with every page request to the same server. Cookies are the best
way to store state data that must be available for multiple Web pages on a
web site.
E. Query Strings - Query strings store values in the URL that are visible
to the user. Use query strings when you want a user to be able to e-mail or
instant message state data with a URL.
<a href="name.asp?name=Paco">Paco</a><br>
Request.Redirect(“Default.aspx”,Username?”+ Username) – On .CS Page
response.redirect("page1.aspx?id=xxx&name=xxxx&phonexxxxx"); - On multiple
values
Retrieving value
name=Request.QueryString("name")

2. Server – Side State Management


a. Application State - Application State information is available to all pages,
regardless of which user requests a page.
b. Session State – Session State information is available to all pages opened by a
user during a single visit.
Both application state and session state information is lost when the
application restarts. To persist user data between application restarts, you can
store it using profile properties.

Difference between Application State and Session?


Application state is a state where we have the information that is global
for the application where as session state is a state which is maintained as a per-
user basis.

Modes in Session State?


Three type of session state mode available in Asp.Net---
1) In Proc: - In this mode session kept as line objects in web server
(aspnet_wp.exe). It used cookiless configuration in web.config to munge the
session ID on to the URL.
2) State Server: - In this mode session is serialized and stored in the
memory in a separate process (aspnet_state.exe). It can be run on another
mechine.
3) SQL Server: - In this mode session data is stored in the SQL Server.
Mainly this mode is used in such a scenario when the session data is so
important. It can be stored anywhere in the Clustered Server mode. So, if a SQL
Server is restarted it can be retrieved.
Performance Issue:-
In Proc: - It works fast, but if more session data, more memory consume
on web server. State Server:- when storing data of basic type (string,int etc), it is
15% slower than In Proc. However cost of serialization/deserialization can affect
performance, But it can store lots of objects. SQL Server:- It is 25% slower than
In Proc.
Rules:-
In Proc:- It supports session_end event. But it cannot work in the web
garden mode, because in the web garden mode multiple aspnet_wp.exe will be
running on the same machine.
State Server:-In a web form you must have the same as in your web
server. Object must be serializable .To access the state data in different web
server the application path of the web sites in the IIS metabase should be
identical.
SQL Server:-If you specify integrated security in the connection string
(eg. "Trusted-Connection=true" or "Integrated security=true") or turn on
Impersonation in asp.net it would not be work.

What is a Session ID?


• A session ID is a unique number that a Web site's server assigns a specific
user for the duration of that user's visit (session). The session ID can be
stored as a cookie, form field, or URL (Uniform Resource Locator).

• Every time an Internet user visits a specific Web site, a new session ID is
assigned. Closing a browser and then reopening and visiting the site again
generates a new session ID.

What is Cookies?
Cookies are small pieces of information stored in the client computer.
They are limited to 4K in size.
Cookies are two types:
1. In-memory cookies also called as Session cookies or Non-Persistent cookies
These cookies are saved in memory and will be lost while closing your browser.
2. Persistent cookies
A persistent cookie is saved as a text file in the file system of the client
computer usually under Temporary Internet Files folder. To create persistent
cookie we need to set cookie Expires property. Generally we go for persistent
cookies to implement the features like “Remember me” option OR to store user
information such as selected theme.
Example 1:

HttpCookie userInfo = new HttpCookie("userInfo");


userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0));
Response.Cookies.Add(userInfo);

Example 2:

Response.Cookies["userName"].Value = "Annathurai";
Response.Cookies["userColor"].Value = "Black";

ASP.NET PAGE LIFE CYCLE:


Stage Events /Method
Initialization of the page Page_Init
Loading of the View State LoadViewState
Processing of the Post back data LoadPostData
Loading of Page Page_Load
Notification of PostBack RaisePostDataChangedEvent
Handling of PostBack Event RaisePostBackEvent
Pre Rendering of Page Page_PreRender
Saving of view state SaveViewState
Rendering of Page Page_Render
Unloading of the Page Page_UnLoad

What is IIS?
Internet Information Services is used to make your computer a web server.
If we want to have a web server for developing dynamic website or want to
publish website on our own server then we install the IIS. IIS is used on windows
plate form. For other plate form we have different web servers. E.g. apache for
Linux.

If there is an error in a application and if it need to be directed to a


particular page while at runtime It needs to be configured at web.config file
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm"
</customErrors>
CONTROLS

What is web server control?


Web server controls are just like HTML controls. Web controls are
easily identified by the fact that they have a runat="server" attribute. They are
also handled at the server by the ASP.NET runtime when a page containing them
is requested. They can expose and raise server events which you can use to
interact with them. They are identified by giving them an id attribute which you
can then use to reference the control in your code.

What are the 3 types of custom controls in ASP.NET?


Microsoft Visual Studio .NET provides three types of custom control for use on
Web forms.
1. Web user controls
This combine existing server and HTML controls by using the Visual
Studio .NET Designer to create functional units that encapsulate some aspect of
the user interface. User controls reside in content files, which must be included
in the project in which the controls are used.
2. Composite custom controls
These create new controls from existing server and HTML controls.
Although similar to user controls, composite controls are created in code rather
than visually, and therefore they can be compiled into an assembly (.dll), which
can be shared between multiple applications and used from the Toolbox in Visual
Studio .NET.
3. Rendered custom controls
These create entirely new controls by rendering HTML directly rather than
using composition. These controls are compiled and can be used from the
Toolbox, just like composite controls, but you must write extra code to handle
tasks that are performed automatically in composite controls.

What are the 3 Register directives’ attributes?


Tag Prefix
Namespace
Assembly

What are the differences between User Controls and Custom Controls?
1. User Controls are easy to create where as Custom Controls are difficult to
create.
2. User Controls cannot be compiled into an assembly; where as Custom
Controls can be compiled into an assembly.
3. User Controls cannot be added to tool box; where as Custom controls can be
added to the toolbox.
4. You need to have a copy of user control in every project where you want to
use it, where as this is not the case with custom controls. You can install a single
copy of the Web custom control in the global assembly cache and share it
between applications, which make maintenance easier.
5. User controls are used for reusing existing user interface elements and code,
but are not useful for developing reusable components for multiple web
applications.
What is Page Rendering?
Page rendering is the process of generating a page from the database.
Use the Page Rendering section to modify controls that impact the rendering of a
page, including page attributes, regions, buttons, items, page rendering
computations, and page processes.

Stage Meaning Typical uses


ASP.NET The page's Page_Init During this event, the ASP.NET page framework
Page event is raised, and the restores the control properties and postback
Framework page and control view data.
Initialization state are restored.
User Code The page's Page_Load Read and restore values stored previously:
Initialization event is raised.
• Using the Page.IsPostBack property,
check whether this is the first time the
page is being processed.
• If this is the first time the page is being
processed, perform initial data binding.
• Otherwise, restore control values.

• Read and update control properties.


Validation The Validate method of (There is no user hook at this stage. You can test
any validator Web server the outcome of validation in an event handler.)
controls is invoked to
perform the control's
specified validation.
Event If the page was called in Perform your application-specific processing:
Handling response to a form
event, the corresponding • Handle the specific event raised.
event handler in the
Note Events are not raised in a
page is called during this
particular order, except that cached
stage. control events — as specified by the
control's AutoPostBack property — are
always processed before the posting
event.

• If the page contains Types of


Validation for ASP.NET Server Controls,
check the IsValid property for the page
and for individual validation controls.
• Manually save the state of page
variables that you are maintaining
yourself.
• Check the IsValid property of the
page or of individual validation controls.
• Manually save the state of controls
dynamically added to the page.
Cleanup The Page_Unload event Perform final cleanup work:
is called because the
page has finished • Closing files.
rendering and is ready to • Closing database connections.
• Discarding objects.
be discarded.

Note It is important that expensive


resources, such as database connections,
be explicitly closed. Otherwise, they will
remain open until the next garbage
collection occurs. On a heavily loaded
server, many open resources can
adversely affect performance.

Explain 3 tire Architecture?


Presentation Layer (UI)
Presentation layer contains pages like .aspx or windows form where data is
presented to the user or input is taken from the user.
Business Access Layer (BAL) or Business Logic Layer
BAL contains business logic, validations or calculations related with the data, if
needed. I will call it Business Access Layer in my demo.
Data Access Layer (DAL)
DAL contains methods that helps business layer to connect the data and perform
required action, might be returning data or manipulating data (insert, update,
delete etc).
What i s difference between trace and debug?
Trace Debug

This class works only when your This class works only when your
application build defines the symbol application build defines the symbol
TRACE. DEBUG.

For tracing, you have to use For tracing, you have to use
Trace.WriteLine statements. Debug.WriteLine statements.

Trace class is generally used to trace the You generally use debug classes at
execution during deployment of the the time of development of
application. application.

Trace class works in both debug mode Debug class works only in debug
as well as release mode. mode.

Performance analysis can be done using Performance analysis cannot be done


Trace class. using Trace class.

Trace runs in a thread that is different Debug runs in the same thread in
from the Main Thread. which your code executes.

Trace is used during Testing Phase and Debug is used during Debugging
Optimization Phase of different releases. Phase
What is Tracing?
Tracing is a way to monitor the execution of your ASP.NET application. You
can record exception details and program flow in a way that doesn't affect the
program's output.
Page level Tracing

ASP.NET tracing can be enabled on a page-by-page basis by adding "Trace=true"


to the Page directive in any ASP.NET page:

<%@ Page Language="C#" Trace="true" TraceMode =


"SortByCategory" Inherits = "System.Web.UI.Page"
CodeFile="Default.aspx.cs" %>

Additionally, you can add the TraceMode attribute that sets SortByCategory or
the default, SortByTime. You can use SortByTime to see the methods that take
up the most CPU time for your application. You can enable tracing
programmatically using the Trace.IsEnabled property.

Application Tracing

You can enable tracing for the entire application by adding tracing settings in
web.config. In below example, pageOutput="false" and requestLimit="20" are
used, so trace information is stored for 20 requests, but not displayed on the
page because pageOutput attribute is set to false.

<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" />
<authentication mode="Windows" />
<trace enabled ="true" pageOutput ="false" requestLimit ="20"
traceMode ="SortByTime " />
</system.web>
</configuration>
The page-level settings take precedence over settings in Web.config, so if
enabled="false" is set in Web.config but trace="true" is set on the page, tracing
occurs.
Trace.Write(String Category,String Message, Exception errorinfo);
Trace.Warn(String Category,String Message, Exception errorinfo);

To enable paging it need to be implemented in <%@ Page Trace =


“True “>
What is Difference between Error and Exception?
Error: Any departure from the expected behavior of the system or program
which stops the working of the system is an error. In general error is which
nobody can control or guess when it occurs.
Exception: Any error or problem which one can handle and continue to work
normally. Exception can be guessed and can be handled.
ADO.NET
What is the difference between DataReader and DataAdapter?
1. Data Reader is read only forward only and much faster than
DataAdapter. If you use DataReader you have to open and close
connection explicitly where as if you use DataAdapter the connection is
automatically opened and closed.
DataReader is connection oriented where as Data Adapter is
disconnected

Some Data Provider Objects?


•Command object which is used to store procedures.
•Data Adapter which is a bridge between datastore and dataset.
•Datareader which reads data from data store in forward only mode.
•A dataset object is not in directly connected to any data store. It represents
disconnected and cached data. The dataset communicates with Data adapter
that fills up the dataset. Dataset can have one or more Datatable and relations.
•DataView object is used to sort and filter data in Datatable.

Data View:
The Data filtration in dataset can be done using data view.

DataSet ds = getdatas.dataview();//The Fucntion Returns Dataset.


DataView dv1 = ds.Tables[0].DefaultView;//Filter it according to the Table
dv1.RowFilter = ("CustomerID = 'ALFKI'");
dv.Sort = ("CategoryName");

//
DataView dv = new DataView(ds.Tables[0], "CategoryID > '2'", "CategoryID",
DataViewRowState.CurrentRows);

DataView.RowFilter = "SalesPerson = 'Bill' AND SaleDate > '1/1/1995'


"

DataSet ds = new DataSet();


ds.ReadXml(MapPath("XMLFile.xml"));
dgrdMenu.DataSource = ds;
dgrdMenu.DataBind();

DetailsView and FormView Controls:


----------- --- -------- --------
The DetailsView and FormView controls, these controls enable us to work
with a single data item at a time. Both controls enable you to display, edit,
insert, and delete data items such as database records. Furthermore, both
controls enable you to page forward and backward through a set of data items.
The difference between the two controls concerns the user interface that the
controls render. The DetailsView control always renders each field in a separate
HTML table row. The FormView control, on the other hand, uses a template that
enables you to completely customize the user interface rendered by the control.

A DetailsView control renders an HTML table that displays the contents of a


single database record. The DetailsView supports both declarative and
programmatic databinding.

You can use the FormView control to do anything that you can do with the
DetailsView control. Just as you can with the DetailsView control, you can use the
FormView control to display, page, edit, insert, and delete database records.
However, unlike the DetailsView control, the FormView control is entirely
template driven

The FormView control provides you with more control over the layout of a form.
Furthermore, adding validation controls to a FormView is easier than adding
validation controls to a DetailsView control.
Details View Form View

Repeater and DataList Controls:


-------- --- -------- --------

Both the Repeater and Data List controls helps us to display a set of data
items at a time. For example, you can use these controls to display all the rows
contained in a database table.

The Repeater control is entirely template driven. You can format the rendered
output of the control in any way that you please. For example, you can use the
Repeater control to display records in a bulleted list, a set of HTML tables, or
even in a comma-delimited list.

The DataList control is also template driven. However, unlike the Repeater
control, the default behavior of the DataList control is to render its contents into
an HTML table. The DataList control renders each record from its data source
into a separate HTML table cell.
The Repeater control provides you with the maximum amount of flexibility in
rendering a set of database records. You can format the output of the Repeater
control in any way that you please.

The DataList control, like the Repeater control, is template driven. Unlike the
Repeater control, by default, the DataList renders an HTML table. Because the
DataList uses a particular layout to render its content, you are provided with
more formatting options when using the DataList control.

What is a Gridview?
The Gridview Web server control is a powerful tool for displaying
information from a data source. It is easy to use; you can display editable data in
a professional-looking grid by setting only a few properties. At the same time,
the grid has a sophisticated object model that provides you with great flexibility
in how you display the data.

What is typed Dataset?


A typed dataset is very much similar to a normal dataset. But the
only difference is that the schema is already present for the same. Hence any
mismatch in the column will generate compile time errors rather than runtime
error as in the case of normal dataset. Also accessing the column value is much
easier than the normal dataset as the column definition will be available in the
schema.

Das könnte Ihnen auch gefallen