Data source controls in ASP. NET

Source: Internet
Author: User

In this article, I will guide you through the main data source controls in ASP. NET 2.0

Data source components

Basically, the data source control is a Server control that encapsulates some basic functions of specific data sources such as SQL Server, Microsoft Access, or XML documents. These basic functions include query, insert, update, and delete. Data source controls do not have a UI and do not display any tags. Note that many controls do not have a UI, but are rendered with tags ). You can bind it to a data control through declaration or programming. Through the service of the data source component, the data binding control can not only obtain data from a specific data source, but also insert new records or update and delete existing records. Control interfaces are always the same, no matter which data source you use, whether it is an SQL Server database, XML document, Microsoft Excel worksheet or site chart description ).

The data source object is self-descriptive and allows the bound control to understand the supported functions in the basic data source. In this way, the control can easily adjust its own user interface based on the function of the data source it connects. For example, the grid component can only display the Edit column when the basic data source is editable.
The data source control is the most important change to date in the ASP. NET 2.0 data binding model. In ASP. NET 2.0, the data source control is a recommended tool for data binding. Note that the data source control works with the object that exposes the IEnumerable interface. In data-driven applications, data source controls will never replace DataView and arrays. In addition, backward compatibility can be ensured. With the data source control, you now have an alternative method to bind data to any newly added and existing data binding controls. The data source control does not cause any performance problems. The performance is basically the same as or even slightly higher than version 1.x.

ASP. NET 2.0 adds a new property to all data binding controls so that each control can be successfully bound to the data source control. Set the new property performanceid to the name of the data source control defined on the same page. The following code snippet shows how to fill the DataGrid Control with the query results executed for the SQL Server database:

 
 
  1. <asp:SqlDataSource 
  2. runat="server" 
  3. ID="MySource" 
  4. ConnectionString="...;" 
  5. DataSourceMode="DataSet" 
  6. SelectCommand="..." 
  7. /> 
  8. <asp:DataGrid 
  9. runat="server" 
  10. ID="data" 
  11. DataSourceId="MySource" 
  12. /> 

SqlDataSource is a data source control that exposes SQL relational database content. It should be noted that SqlDataSource is not specific to SQL Server, but the details about this issue will be discussed later .) The ConnectionString attribute identifies the source database, and the SelectCommand attribute is set as a query string. As mentioned above, you can use the traditional DataSource attribute or the new performanceid attribute to pass data to the data binding control. Note that these two attributes are mutually exclusive. If you set both attributes, an exception is thrown.

What do you get by using a data source control instead of a traditional enumerated object? First, you can use a simple tag on the. aspx page to declare the data source. In this way, the data source object can be automatically instantiated, and the number of code to be written to complete the page can be reduced. You no longer need to explicitly manipulate objects such as SqlConnection and SqlCommand. The data source reduces the dependency on server data component connections, adapters, and typed datasets because these components depend heavily on Visual Studio. NET code generation. In Visual Studio 2005, the number of automatically generated code that is isolated in the don't-change-this-code area is significantly reduced. This does not mean to discard the design-time function, but is the opposite. Data source controls enable direct and implicit binding between controls and data components. With this integrated system structure, you can develop an intelligent designer to dynamically discover the architecture and data, so as to more accurately express the runtime appearance of the data-bound control.

At least for common scenarios such as selection, sorting, paging, deletion, and basic updates, you can set data binding by simply connecting and configuring a pair of controls. Figure 2 shows the Data tab of the Visual Studio 2005 toolkit. It contains some data source controls and data binding controls-the only tool you need in many cases. In this case, almost no data binding code is required for your page. However, because pages require more complex data binding functions, you need to add a small amount of code.

The data source control allows you to implement consistent binding models among multiple data sources. The control in Figure 2 is only a subset of the controls available when ASP. NET 2.0 is released .) As a page developer, you will use the same attributes, regardless of whether the data source is a relational table or a database system), XML documents, custom classes, or Excel files.

Another advantage you get by using the data source control is related to data cache. Many books and articles on ASP. NET coding policies and optimization list cached data as the best practice for building high-performance, scalable Web applications. There is no doubt that data caching means key performance enhancements, even if it is not a magic wand that works for all pages and applications. For example, when you manage a large amount of session-specific unstable data, or when your requirements require that new data be always displayed, extensive use of cache may not be the best method. In most cases, cache data is a way to improve applications.

The data source control also integrates the cache function, and enabling and disabling the default cache function is as easy as setting the Boolean attribute. Cache is transparent for data binding controls, and the data source control manages some of its aspects, such as generating cache keys, expiration Policy times, and key dependency timestamps ). Other settings are determined by PAGE developers, including the lifetime of data in the cache. Note that the data source control maintains a separate cache for each unique connection string, query selection, parameter, and cache setting combination.

It should also be noted that some data source controls, especially SqlDataSource, support data cache expiration, that is, they can detect database changes and make the current cached data expire. Later, you will see that this function requires the basic SQL storage area to provide specific support.

The use of data source controls, rather than traditional enumerated objects, has many advantages. Let's take a look at some of its shortcomings. As mentioned previously, in ASP. NET 2.0, each data binding control supports double APIs for data binding. These APIs are almost completely isolated from each other and basically have nothing in common. At first glance, it seems that the data source control supports no code binding, and you can complete visual programming by pointing to and clicking. Undoubtedly, you can create a data-driven page without writing any code. However, this does not mean that the data binding control does not allow you to hook internal events. The new architecture is more automated, But it retains all aspects of the explicit data binding model used in ASP. NET 1.x.

All in all, the main difference between the two models is that when data access is performed, the data source control acts as a proxy. If the active data source object is critical to your application, consider initiating a pair of pre-and post-operation events such as the Deleting/Deleted event for each supported data operation ). This gives you exactly the same control over data streams as ASP. NET 1.x, but this time it is implemented through a much simpler and more compact syntax.

Finally, remember that data source controls are just a group of classes. Therefore, you can fully control them and instantiate and manipulate them programmatically. In this case, you use the data source control as a more abstract API that works on the original ADO. NET class. To some extent, the Data source control represents the development of the Data Application block, which is. NET Framework 1. the API introduced by x aims to implement common ADO. NET best practices, and reduce the amount of code you need to write.

  1. DataList and Repeater controls of ASP. NET
  2. Analysis of IIS ing in ASP. NET
  3. Overview ASP. NET status types
  4. Introduction to ASP. NET and Web servers
  5. EnableViewState attribute of ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.