November 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Check Out The ASP.NET 2.0 Whidbey Features!

  • October 27, 2003
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Sharing Code in the Application

Before ASP.NET 2.0, if you were to reference a reusable component from your ASP.NET application, you had to compile the assembly and place it in the bin folder (or place it in the GAC) of the Web application. But now, with ASP.NET 2.0, creating a reusable component is very simple and straightforward. All you need to do is to create the component in a pre-defined subdirectory called Code. Any component placed in this directory will be automatically compiled at runtime into a single assembly. This assembly is automatically referenced and will be available to all the pages in the site. Note that you should only put components in the Code subdirectory.

Creating Web Portals Using Web Parts

There are many times where you would want to allow the users of your Web site to be able to customize the content by selecting, removing, and rearranging the contents of the Web page. Traditionally, implementing this capability required a lot of custom code or you had to depend on third-party products to build a Web site of this nature. To address this shortcoming, ASP.NET 2.0 ships with a Web Parts Framework that provides the infrastructure and the building blocks required for creating modular Web pages that can be easily customized by the users. You can also use Web Parts to create portal pages that aggregate different types of content such as static text, links, and content that can change at runtime. It is also possible for the users to change the layout of the Web parts by dragging and dropping from one place to another, providing a rich user experience.

New Data Controls

One of the limitations of ASP.NET 1.0 is that it did not provide a declarative model for binding data to data-aware controls such as DataGrid, DataList, and Repeater. Now, in ASP.NET 2.0, you have a very powerful and easy-to-use declarative model for binding data directly from the database. To this end, ASP.NET 2.0 provides a number of new data-bound controls. Before we take a look at this new set of controls, let us understand the theory behind this new declarative model. Binding data to data-aware controls requires the following two steps:

  1. You need to create a data source control that is responsible for reading and writing data from the database. Data source controls encapsulate common functionality such as reading, writing, deleting, and inserting from the database.
  2. Next, you need to bind the data-bound controls, such as a dropdown list or a checkbox, to the data source control.

ASP.NET 2.0 provides the following data source controls:

  • <asp:SqlDatasource>—This data source control is designed to work with SQL Server, OLE DB, ODBC, and Oracle databases. As the name suggests, this control enables you to select, update, delete, and insert data using SQL commands. In the later section, we will see an example of this control.
  • <asp:ObjectDatasource>—For reasons of clean separation and easier maintenance, most of the Web applications are constructed using N-Tier principles. When you work with an N-tier application, it is most likely that your middle layer objects may return complex objects that you have to process in your ASP.NET presentation. Keeping this in mind, Microsoft has created this new control that allows you to seamless integrate the data returned from the middle layer objects with the ASP.NET presentation layer.
  • <asp:AccessDatasource>—As you can see from the name, this control is designed to work with Access databases.
  • <asp:XmlDatasource>—This control allows you to bind to XML data, which can come from a variety of sources such as an external XML file, a DataSet object, and so on. Once the XML data is bound to the XmlDataSource control, this control then can act as a source of data for data-bound controls such as TreeView and Menu.
  • <asp:DataSetDatasource>—The DataSetDataSource control allows you to easily switch between an XML and a relational view of the data. By using this control, you also can specify an XSLT Transformation to restructure the XML data.
  • <asp:SitemapDatasource>—This control allows the users to navigate between the pages in the Web site. To perform this, you need to create an XML file named app.sitemap that lays out the pages of the site in a hierarchical fashion. Once you have the site hierarchy in the app.sitemap file, you then can data-bind the SitemapDataSource control with the app.sitemap file. After that, the contents of the SitemapDataSource control can be bound to data-aware controls such as TreeView and so on.

Apart from the above data source controls, ASP.NET 2.0 also provides the following data-bound controls that you will normally use to display data that is contained in the data source controls:

  • <asp:gridview>—This control is the successor to the DataGrid control that was part of ASP.NET 1.0. Like the DataGrid control, this control is used to display the values of a data source in a table. In a grid view control, each column represents a field, while each row represents a record. As you would expect, you can bind a grid view control to a SqlDataSource control, as well as any data source that implements the System.Collections.IEnumerable interface. This control also can adaptively render data for different types of devices and browsers that are making the request.
  • <asp:detailsview>—As the name suggests, this control can be used in conjunction with the grid view control and can be used to display the details of a specific record in the data source.

The following example demonstrates how to use the combination of SqlDataSource and GridView controls to retrieve and display data from the Categories table in the Northwind database without writing even a single line of code.

<%@ page language="C#" %>
<script runat="server">
</script>

<html>
<head id="Head1" runat="server">
   <title>Data Binding using SqlDataSource control</title>
</head>
<body>
  <form id="Form1" runat="server">
    Enter the Category ID: <asp:textbox id="CategoryID"
                           runat="server"></asp:textbox>
    <asp:button runat="server" id="btnGet" text="Get Category"/>

    <asp:sqldatasource id="categoriesSource" runat="server"
    connectionstring="server=localhost;database=northwind;
                      uid=sa;pwd="
    selectcommand="SELECT * From Categories
                            Where CategoryID=@CategoryID">
         <SelectParameters>
         <asp:ControlParameter defaultvalue="1" Name="CategoryID"
              ControlId="CategoryID" PropertyName="Text"/>
         </SelectParameters>
      </asp:sqldatasource>
      <asp:gridview datasourceid="categoriesSource"
                    runat="server" id="gridCategories">
      </asp:gridview>
  </form>
</body>
</html>

In the above code, we start by declaring a button control named btnGet. This control is just used to perform a post-back to the server so that the SQL query can be re-evaluated. Then, we declare a SqlDataSource control named categoriesSource. While declaring the SqlDataSource control, we also specify the ConnectionString and the SQL statement to be executed as attributes. As you can see from the SelectCommand attribute value, we specify a placeholder for the CategoryID parameter by using the @CategoryID identifier. Then, we specify the value for the SQL query parameter by using the SelectParameters template. The combination of ControlId and PropertyName is used to specify the name of the control and the property of the control (that will return the value of the textbox in this case). This allows the category ID entered in the CategoryID textbox to be used as an argument to the SQL query. For the first time, we specify the default value of 1 for the CategoryID by using the defaultvalue property.

When you execute the above code, you will see an output that is somewhat similar to the following.

By default, when the above page comes up, it displays the details of the category that is identified by CategoryID 1. After that, if you enter a category ID in the textbox and click on Get Category, it will display the details of the category based on the value entered.

Advanced Features of Data Controls

Some of the advanced features provided by the data controls are as follows:

  • By handling all the low level tasks such as opening connection to the database, executing a command, retrieving the results of the command, and closing the connection, data source controls allow you to focus on the core business logic of the application.
  • Apart from providing a powerful declarative model for creating rich data-driven Web applications, ASP.NET 2.0 also allows you to access the same set of features programmatically, retaining the flexibility of the database features.
  • By using the declarative model provided by data controls, not only you can execute stored procedures but also pass parameters to the stored procedures. For example, you can retrieve the value of a Textbox and pass it as a parameter to a stored procedure that can return appropriate rows depending on the value entered in the textbox.
  • Another excellent feature of data source controls is that they all offer the same basic Object model and API for you to program against, reducing the learning curve required for understanding the different types of controls.

Caching Enhancements

The Cache API introduced with ASP.NET 1.0 was a powerful feature that can be immensely useful in increasing the performance of a Web application. The Cache API also allows you to invalidate items in the cache based on some pre-defined conditions such as change in an XML file, change in another cache item, and so on. By using this feature, you can remove or invalidate an item from the cache when the data or another cached item changes. However, the Cache API in ASP.NET 1.x versions did not provide a mechanism to invalidate an item in the cache when data in a SQL Server database changes. This is a very common capability that many Web applications require.Now, with ASP.NET 2.0, Microsoft has introduced a new cache invalidation mechanism that works with SQL Server as well. By using this new capability, you can invalidate an item in the Cache object whenever the data in a SQL Server database changes. This built-in cache invalidation mechanism works with SQL Server 7.0 and above. However, with SQL Server 7.0 and 2000, only Table-level cache invalidation mechanism is supported.

The next release of SQL Server (code-named Yukon) also will feature a row-level cache invalidation mechanism providing a finer level of accuracy over the cached data. To enable the SQL Server-based cache invalidation mechanism, you need to do the following:

  • Add the OutputCache directive to the top of the page. The OutputCache directive also contains a new attribute named sqldependency and the value supplied to this attribute should be of the form <DatabaseName>:<TableName>. In this example, we are using the Employees table in the Northwind database.
    <%@ OutputCache duration="3600" varybyparam="none"
                    sqldependency="Northwind:Employees" %>
    
  • The next step is to add the appropriate settings to the web.config file. After modification, the web.config file should look like the following:
    <?xml version="1.0"?>
    <configuration>
       <connectionStrings>
           <add name="Northwind"
            connectionString="server=localhost;database=Northwind;
                              UID=sa;PWD="/>
       </connectionStrings>
       <system.web>
          <cache>
            <sqlCacheDependency enabled = "true"
                                pollTime = "1000" >
                  <databases>
                     <add name="Northwind"
                          connectionStringName="Northwind"
                          pollTime = "1000"/>
                  </databases>
            </sqlCacheDependency>
          </cache>
       </system.web>
       ---------
       ---------
    </configuration>
    

    By using the above configuration entries, we specify the name of the database in which we want to enable the cache notification mechanism.

  • After that, we need to enable the specific tables in the Northwind database for notification. You can perform this using any one of the following two ways:
    • Using the aspnet_regsqlcache
    • Using the EnableTableForNotifications method of the SqlCacheDependencyAdmin class. Once we configure the table to send notifications, any time data in the table changes, it notifies the ASP.NET to invalidate the specific item in the cache.




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel