Tuesday, February 14, 2006

UPGRADE ASP.NET 2.0: New Code Behind Model

In ASP.NET 1.x, the code-behind file was a class file that inherited the System.Web.UI.Page class, and the web page inherited the code-behind file. As a result, all the controls that appeared on the page were declared in the code-behind file so they would be available to both the code-behind file and page file.

Visual studio added a declaration to the code-behind file for each control. This declaration was placed in a region of the code-behind file which hidden. It was to hard to sync code-behind with page. Any control deletion, change in control name in page file might not affect the code region in code-behind file.

The new code-behind file not includes any hidden code region. Instead, the new model uses a partial class feature in VB 2005. Due to this control declarations are placed in separate file that's compiled together with the code-behind file to create final code-behind class file. All declarations created at compiled time by ASP.NET. So no mess-ups occurred.

Advantage is easier to keep the web page file in synchronization with the code-behind file.

4 comments:

Anonymous said...

hello kuldeep,
nice blogs u have put to help developers....!!!

kuldeep please help me since i am a novice and have not got the nuts and bolts of asp.net 2.0 ............ my email id is busywiz@yahoo.co.in
please help me with the actual procedure to connect to database with code behind and how to build a large appliction in asp.net

KD said...

Thanks for comments.
Very Soon I'll publish here way to connect database and I'll forward you link for the same.
By the way to bulid a larger scalable application in ASP.NET, it's not very small topic to discuss. But for this case you can take help of Microsofts official website:

www.asp.net

Here, you can find tutorials, fourum (that really help you to find out solutions, and many more)

Regards

Kuldeep

KD said...

To connect with database,
1. First create a entry for connection string in web.config file in connectionStrings section, as shown below:
connectionStrings
add name="ConnectionName" connectionString="Dsn=DSNName"
providerName="System.Data.Odbc" /
/connectionStrings
Here, for example I used Odbc provider. But you can use other providers like Oledb. To know the connection string format for other providers refer to www.connectionstrings.com

2. Later to retrieve connection string from web.config. Use following code:

ConfigurationManager.ConnectionStrings ("ConnectionName").ConnectionString

3. Further you can use this connection string with connection object.
Dim cmd As OdbcCommand = _
New OdbcCommand(sql, New OdbcConnection(ConfigurationManager.ConnectionStrings ("ConnectionName").ConnectionString))
cmd.Connection.Open()

Regards

HTH

codaham said...

I'd been trying to find the solution to a problem I was having for an hour. This was about the 20th article I read about it. This was the first one that worked!!

Thanks for your help!!

Visitor Count: