ASP.NET Page Life Cycle Overview


ASP.NET Page Life Cycle Overview

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important for you to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you intend.
Common Life-cycle Events
Page Event
Typical Use
PreInit
Raised after the start stage is complete and before the initialization stage begins.
Use this event for the following:
· Check the IsPostBack property to determine   whether this is the first time the page is being   processed.   . The IsCallback and IsCrossPagePostBack properties   have also been set at this time.
· Create or re-create dynamic controls.
· Set a master page dynamically.
· Set the Theme property dynamically.
· Read or set profile property values.
Init
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page.
Use this event to read or initialize control properties.
InitComplete
Raised at the end of the page's initialization stage. Only one operation takes place between the Init andInitComplete events: tracking of view state changes is turned on. View state tracking enables controls to persist any values that are programmatically added to the ViewState collection. Until view state tracking is turned on, any values added to view state are lost across postbacks. Controls typically turn on view state tracking immediately after they raise their Init event.
Use this event to make changes to view state that you want to make sure are persisted after the next postback.
PreLoad
Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
Load
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
Use the OnLoad event method to set properties in controls and to establish database connections.
Control events
Use these events to handle specific control events, such as a Button control's Click event or a TextBoxcontrol's TextChanged event.
LoadComplete
Raised at the end of the event-handling stage.
Use this event for tasks that require that all other controls on the page be loaded.
PreRender
Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls. (To do this, the Page object calls EnsureChildControls for each control and for the page.)
The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page.
Use the event to make final changes to the contents of the page or its controls before the rendering stage begins.
PreRenderComplete
Raised after each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.
SaveStateComplete
Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.
Render
This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.
If you create a custom control, you typically override this method to output the control's markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method. For more information, see Developing Custom ASP.NET Server Controls.
A user control (an .ascx file) automatically incorporates rendering, so you do not need to explicitly render the control in code.
Unload
Raised for each control and then for the page.
In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.
For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.

ASP.NET 2.0 Interview Questions – (Intermediate)

What is XHTML? Are ASP.NET Pages compliant with XHTML?

In simple words, XHTML is a stricter and cleaner version of HTML. XHTML stands for EXtensible Hypertext Markup Language and is a W3C Recommendation.

Yes, ASP.NET 2.0 Pages are XHTML compliant. However the freedom has been given to the user to include the appropriate document type declaration.

More info can be found at http://msdn2.microsoft.com/en-us/library/exc57y7e.aspx

State Management in ASP.NET



State management is the process by which you maintain state and page information over multiple requests for the same or different pages.

Types of State Management

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:


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.
Sample Code
//To Save Information in View State
ViewState.Add("Cherukuri""Venkateswarlu");
//Retrieving View state
Label1.Text = (string)ViewState["Cherukuri"];

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.


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.
Sample Code
//Declaring a hidden variable
HtmlInputHidden hidCherukuri = null;
//Populating hidden variable
hidCherukuri.Value = "Venkateswarlu";
//Retrieving value stored in hidden field.
Label1.Text = hidCherukuri.Value;

New Features in ASP.NET 4 and Visual Studio 2010 Web Development




Core Services 
  • Webconfig File Refactoring
  • Extensible Output Caching
  • Auto-Start Web Applications
  • Permanently Redirecting a Page
  • Shrinking Session State
  • Expanding the Range of Allowable URLs
  • Extensible Request Validation
  • Object Caching and Object Caching Extensibility
  • Extensible HTML, URL, and HTTP Header Encoding
  • Performance Monitoring for Individual Applications in a Single Worker Process
  • Multi-Targeting
Ajax
  • jQuery Included with Web Forms and MVC
  • Content Delivery Network Support
  • ScriptManager Explicit Scripts
Web Forms
  • Setting Meta Tags with the PageMetaKeywords and PageMetaDescription Properties
  • Enabling View State for Individual Controls
  • Changes to Browser Capabilities
  • Routing in ASPNET 4
  • Setting Client IDs
  • Persisting Row Selection in Data Controls
  • ASPNET Chart Control
  • FiltFiltering Data with the QueryExtender Control
  • Html Encoded Code Expressions
  • Project Template Changes
  • CSS Improvements
  • Hiding div Elements Around Hidden Fields
  • Rendering an Outer Table for Templated Controls
  • ListView Control Enhancements
  • CheckBoxList and RadioButtonList Control Enhancements CheckBoxList and RadioButtonList Control Enhancements
  • Menu Control Improvements
  • Wizard and CreateUserWizard Controls
ASPNET MVC
  • Annotation Attribute Validation Support
  • Templated Helpers
Dynamic Data
  • Enabling Dynamic Data for Existing Projects
  • Declarative DynamicDataManager Control Syntax
  • Entity Templates
  • New Field Templates for URLs and E-mail Addresses
  • Creating Links with the DynamicHyperLink Control
  • Support for Inheritance in the Data Model
  • Support for Many-to-Many Relationships (Entity Framework Only)
  • New Attributes to Control Display and Support Enumerations
  • Enhanced Support for Filters
Visual Studio 2010 Web Development Improvements
  • Improved CSS Compatibility
  • HTML and JavaScript Snippets
  • JavaScript IntelliSense Enhancements
Web Application Deployment with Visual Studio 2010
  • Web Packaging
  • Webconfig Transformation
  • Database Deployment
  • One-Click Publish for Web Applications

ASP.NET Page Life Cycle Overview


 ASP.NET Page Life Cycle Overview


ASP.NET Page Life Cycle Overview

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important for you to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you intend.
Common Life-cycle Events
Page Event
Typical Use
PreInit
Raised after the start stage is complete and before the initialization stage begins.
Use this event for the following:
· Check the IsPostBack property to determine   whether this is the first time the page is being   processed.   . The IsCallback and IsCrossPagePostBack properties   have also been set at this time.
· Create or re-create dynamic controls.
· Set a master page dynamically.
· Set the Theme property dynamically.
· Read or set profile property values.
Init
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page.
Use this event to read or initialize control properties.
InitComplete
Raised at the end of the page's initialization stage. Only one operation takes place between the Init andInitComplete events: tracking of view state changes is turned on. View state tracking enables controls to persist any values that are programmatically added to the ViewState collection. Until view state tracking is turned on, any values added to view state are lost across postbacks. Controls typically turn on view state tracking immediately after they raise their Init event.
Use this event to make changes to view state that you want to make sure are persisted after the next postback.
PreLoad
Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
Load
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
Use the OnLoad event method to set properties in controls and to establish database connections.
Control events
Use these events to handle specific control events, such as a Button control's Click event or a TextBoxcontrol's TextChanged event.
LoadComplete
Raised at the end of the event-handling stage.
Use this event for tasks that require that all other controls on the page be loaded.
PreRender
Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls. (To do this, the Page object calls EnsureChildControls for each control and for the page.)
The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page.
Use the event to make final changes to the contents of the page or its controls before the rendering stage begins.
PreRenderComplete
Raised after each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.
SaveStateComplete
Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.
Render
This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.
If you create a custom control, you typically override this method to output the control's markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method. For more information, see Developing Custom ASP.NET Server Controls.
A user control (an .ascx file) automatically incorporates rendering, so you do not need to explicitly render the control in code.
Unload
Raised for each control and then for the page.
In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.
For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.

ADO.NET Interview Questions Part-2



What is Ado.NET?
ñ ADO.NET is an object-oriented set of libraries that allows you to interact with data sources.
ñ ADO.NET is a set of classes that expose data access services to the .NET programmer.
ñ ADO.NET is also a part of the .NET Framework.
ñ ADO.NET is used to handle data access.
What are the two fundamental objects in ADO.NET?
There are two fundamental objects in ADO.NET.
Datareader - connected architecture and
Dataset - disconnected architecture.
What are the data access namespaces in .NET?
The most common data access namespaces :
ñ System.Data
ñ System.Data.OleDb
ñ System.Data.SQLClient
ñ System.Data.SQLTypes
ñ System.Data.XML
What are major difference between classic ADO and ADO.NET?
In ADO the in-memory representation of data is the recordset.A Recordset object is used to hold a set of records from a database table.
In ADO.NET we have dataset.A DataSet is an in memory representation of data loaded from any data source.
what is the use of connection object in ado.net?
The ADO Connection Object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database.
   What are the benefits of ADO.NET?
ñ Scalability
ñ Data Source Independence
ñ Interoperability
ñ Strongly Typed Fields
ñ Performance
What is a Clustered Index?
The data rows are stored in order based on the clustered index key. Data stored is in a sequence of the index. In a clustered index, the physical order of the rows in the table is the same as the logical (indexed) order of the key values. A table can contain only one clustered index. A clustered index usually provides faster access to data than does a non-clustered index.
What is a Non-Clustered Index?
The data rows are not stored in any particular order, and there is no particular order to the sequence of the data pages. In a clustered index, the physical order of the rows in the table is not same as the logical (indexed) order of the key values.
Whate are different types of Commands available with DataAdapter ?
The SqlDataAdapter has
ñ SelectCommand
ñ InsertCommand
ñ DeleteCommand
ñ UpdateCommand
What is the difference between an ADO.NET Dataset and an ADO Recordset?
ñ Dataset can fetch source data from many tables at a time, for Recordset you can achieve the same only using the SQL joins.
ñ A DataSet can represent an entire relational database in memory, complete with tables, relations, and views, A Recordset can not.
ñ A DataSet is designed to work without any continues connection to the original data source; Recordset maintains continues connection with the original data source.
ñ DataSets have no current record pointer, you can use For Each loops to move through the data. Recordsets have pointers to move through them.
Which method do you invoke on the DataAdapter control to load your generated dataset with data?
DataAdapter’ fill () method is used to fill load the data in dataset.
What are the different methods available under sqlcommand class to access the data?
ñ ExecuteReader - Used where one or more records are returned - SELECT Query.
ñ ExecuteNonQuery - Used where it affects a state of the table and no data is being queried - INSERT, UPDATE, DELETE, CREATE and SET queries.
ñ ExecuteScalar - Used where it returns a single record.
What is a DataSet?
A DataSet is an in memory representation of data loaded from any data source.
What is a DataTable?
A DataTable is a class in .NET Framework and in simple words a DataTable object represents a table from a database.
What is the data provider name to connect to Access database?
Microsoft.Access
Which namespaces are used for data  access?
ñ System.Data
ñ System.Data.OleDB
ñ System.Data.SQLClient
What is difference between Dataset. clone and Dataset.copy?
Clone: - It only copies structure, does not copy data.
Copy: - Copies both structure and data.
What is difference between dataset and datareader?
ñ DataReader provides forward-only and read-only access to data, while the DataSet object can hold more than one table (in other words more than one rowset) from the same data source as well as the relationships between them.
ñ Dataset is a disconnected architecture while datareader is connected architecture.
ñ Dataset can persist contents while datareader can not persist contents, they are forward only.
What is DataAdapter?
A data adapter represents a set of methods used to perform a two-way data updating mechanism between a disconnected DataTable and the database. It aggregates four commands: select, update, insert and delete command. One adapter can only generate and fill one table in a DataSet.
What is a Command Object?
The ADO Command object is used to execute a single query against a database. The query can perform actions like creating, adding, retrieving, deleting or updating records.
What is basic use of DataView?
“DataView” represents a complete table or can be small section of rows depending on some criteria. It is best used for sorting and finding data with in “datatable”.
What is the use of Connection Object?
The ADO Connection Object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database.
What are the advantage of ADO.Net?
ñ Database Interactions Are Performed Using Data Commands
ñ Data Can Be Cached in Datasets
ñ Datasets Are Independent of Data Sources
ñ Data Is Persisted as XML.
What is a stored procedure?
A stored procedure is a precompiled executable object that contains one or more SQL statements.
A stored procedure may be written to accept inputs and return output
What is the difference between OLEDB Provider and SqlClient ?
SQLClient .NET classes are highly optimized for the .net / sqlserver combination and achieve optimal results. The SqlClient data provider is fast. It's faster than the Oracle provider, and faster than accessing database via the OleDb layer.
What is the use of Parameter Object?
In ADO Parameter object provides information about a single parameter used in a stored procedure or query.
What is DataAdapter?
DataSet contains the data from the DataAdapter which is the bridge between the DataSet and Database. DataAdapter provides the way to retrieve and save data between the DataSet and Database. It accomplishes this by means of request to the SQL Commands made against the database.
What does ADO mean?
ADO stands for ActiceX Data Objects.It was introduced few years ago as a solution to accessing data that can be found in various forms, not only over a LAN but over the internet. It replaced the data access technologies RDO(Remote Data Objects) and DAO (Data Access Objects).
Name some ADO.NET Objects?
ñ Connection Object
ñ DataReader Object
ñ Command Object
ñ DataSet Object
ñ DataAdapter Object
What is Data Provider?
A set of libraries that is used to communicate with data source. Eg: SQL data provider for SQL, Oracle data provider for Oracle, OLE DB data provider for access, excel or mysql.
What is the DataTableCollection?
An ADO.NET DataSet contains a collection of zero or more tables represented by DataTable objects. The DataTableCollection contains all the DataTable objects in a DataSet.
What are the benefits of ADO.NET?
ADO.NET offers several advantages over previous versions of ADO and over other data access components. These benefits fall into the following categories:
ñ Interoperability
ñ Maintainability
ñ Programmability
ñ Performance
ñ Scalability
How to creating a SqlConnection Object?
SqlConnection conn = new SqlConnection("Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword");
How to creating a SqlCommand Object?
It takes a string parameter that holds the command you want to execute and a reference to a SqlConnection object.
SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);
How to load multiple tables into dataset?
SqlDataAdapter da = new SqlDataAdapter("Select * from Id; Select * from Salry", mycon);
da.Fill(ds);
ds.Tables[0].TableName = "Id";
ds.Tables[1].TableName = "Salary";
What is the provider and namespaces being used to access oracle database?
system.data.oledb
What is the difference between SqlCommand and SqlCommandBuilder?
SQLCommand is used to retrieve or update the data from database.
SQLCommandBuilder object is used to build & execute SQL (DML) queries like select insert update& delete.
What is the use of SqlCommandBuilder?
SQL CommandBuilder object is used to build & execute SQL (DML) queries like select insert update& delete.
What are managed providers?
A managed provider is analogous to ODBC driver or OLEDB provider. It performs operation of communicating with the database. ADO.NET currently provides two distinct managed providers. The SQL Server managed provider is used with SQL server and is a very efficient way of communicating with SQL Server. OLEDB managed provider is used to communicate with any OLEDB compliant database like Access or Oracle.
How do I delete a row from a DataTable?
ds.Tables("data_table_name").Rows(i).Delete
dscmd.update(ds,"data_table_name")
What inside in DataSet?
Inside DataSet much like in Database, there are tables, columns, constraints, relationships, views and so forth.
Explain ADO.Net Architecture?
ADO.NET provides the efficient way to manipulate the database. It contains the following major components. 1. DataSet Object 2. Data Providers :
ñ Connection Object
ñ Command Object
ñ DataReader Object
ñ DataAdapter Object.
What is the difference between int and int32?
Both are same. System.Int32 is a .NET class. Int is an alias name for System.Int32.
What is the role of the DataReader class in ADO.NET connections?
It returns a read-only, forward-only rowset from the data source. A DataReader provides fast access when a forward-only sequential read is needed.
What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET?
SQLServer.NET data provider is high-speed and robust, but requires SQL Server license purchased from Microsoft. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access and Informix. OLE-DB.NET is a .NET layer on top of the OLE layer, so it’s not as fastest and efficient as SqlServer.NET.
What are acid properties?
ñ Atomicity
ñ Consistency
ñ Isolation
ñ Durability
What is DataRowCollection?
Similar to DataTableCollection, to represent each row in each Table we have DataRowCollection.
What is the use of Ado.net connection?
Establishes a connection to a specific data source.
What are basic methods of Dataadapter?
ñ Fill
ñ FillSchema
ñ Update
What are the various methods provided by the dataset object to generate XML?
ReadXML : Read’s a XML document in to Dataset.
GetXML : This is a function which returns the string containing XML document.
WriteXML : This writes a XML data to disk.
What is DataSet Object?
Dataset is a disconnected, in-memory representation of data. It can contain multiple data table from different database.
What is difference between Optimistic and Pessimistic locking?
In Pessimistic locking when user wants to update data it locks the record and till then no one can update data. Other user’s can only view the data when there is pessimistic locking
In Optimistic locking multiple users can open the same record for updating, thus increase maximum concurrency. Record is only locked when updating the record.
What is Execute Non Query?
The ExecuteNonQuery() is one of the most frequently used method in SqlCommand Object, and is used for executing statements that do not return result sets (ie. statements like insert data , update data etc.).
What providers does Ado.net uses?
The .NET Framework provides mainly three data providers, they are Microsoft SQL Server, OLEDB, ODBC.