1. New Features of ASP. NET 2.0 database development:
Compared with previous versions, ASP. NET 2.0 has added more features, greatly improving the efficiency, management, scalability, and performance of developers.
In terms of development efficiency, Asp. NET 2.0 reduces the amount of code for a typical web application by 70%. You can implement the required functions without writing any code in some simple programs, the so-called "0 code ". This is because ASP. NET 2.0 encapsulates common Web tasks into application services and controls that can be easily reused between websites. Using these basic generation blocks is much less user-defined code than in earlier versions to implement many applications.
At the same time, many new server controls are introduced in ASP. NET 2.0 to provide powerful declarative support for data access, login security, wizard navigation, menus, tree views, portals, and so on. Many of these controls use the core application services of ASP. NET for data access, membership and role, and personalized settings.
New features in version 2.0 are as follows:
· Data Source Control-Asp. NET 2.0 introduces declarative data source controls that expose data from backend storage (such as SQL databases, intermediate layer business objects, or XML files) to the Data Binding UI controls on the page. The data source control also provides functions such as sorting, paging, caching, updating, inserting, and deleting data. The UI control can automatically use these functions without code.
· New data binding control-except ASP. except for the data binding control in net 1.x, Asp. net also includes new data binding UI controls such as gridview, detailsview, formview, Treeview, and menu, which can be customized to display data in various formats. The gridview, detailsview, and formview controls can also automatically use the data source function, making it easy to add sorting, paging, and update operations to the data driver page.
· Data Control parameters-the data source can use the data control parameter objects in ASP. NET 2.0 to accept input parameters from various sources. Using these parameter objects, you can easily provide server control property values, sessions, applications, Cookie and querystring fields, and user configuration file attributes to parameterized data operations. With these parameters, you only need a small amount of custom code or no need to customize code to support filtering and master/detail schemes.
· Improved data binding syntax-for common solutions to bind controls to Data Binding templates, The databinder. Eval Data Binding syntax in ASP. NET has been simplified. You can also associate two-way data binding with control properties in the template to allow automatic transfer of values to the data source for update, insert, or delete operations. For Hierarchical XML data, ASP. NET 2.0 also contains an XPATH-based Data Binding syntax.
· Use the SQL express local file database-to simplify development, Asp. NET 2.0 now supports the ability to connect to the SQL express database as a local file in an application, eliminating the need to provide the database to the server for development purposes only. Of course, you can still connect to the server-based SQL database.
The following describes some important data binding controls and data source controls in detail:
· Gridview:
This is a new data binding control in ASP. NET 2.0. It is the subsequent inheritance of the DataGrid Control in ASP. NET 1. X and has made many improvements based on it. Although the DataGrid Control in ASP. NET 1.x allows developers to process some operations, such as sorting, paging, editing, and deleting data through code. But when the gridviwe control is bound to the data source control, it can automatically sort, pagination, edit and delete a series of data processing. In terms of functionality, the gridviwe control also surpasses the DataGrid Control. The gridviwe control supports multiple key fields and provides powerful user customization functions on the UI interface, it also provides developers with a new model for canceling and processing events.
· Sqldatasource:
The sqldatasource control represents a data source control that connects to the sqldatabase provider through ADO. net. Like other data source controls, the sqldatasource control can be bound to any data binding control that supports the performanceid attribute. At the same time, sqldatasource has the ability to describe itself (such as selection, insertion, update, deletion, and sorting), and even the data binding control can automatically implement selection when these capabilities are allowed, insert, update, delete, sort, and so on. The sqldatasource control allows you to replace the large amount of ADO required before creating database connections and implementing queries.. Net code. This is because the data query operation has been directly specified as an attribute of the data source control. Because the data query still exists in the page code, sometimes we call it a dual model.
The sqldatasource control is usually intended for small and personal sites that do not need to query a large number of databases. When we develop a website with large data traffic, we must use the objectdatasource control, this is a data source control developed for large enterprises bound to the intermediate layer object.
The data source control is not rendered, but represents a specific back-end data storage, such as a database, business object, XML file, or XML Web Services. The data source control also supports a variety of data functions (such as sorting, paging, filtering, updating, deleting, and inserting), and the data binding UI control can automatically use these functions. ASP. NET 2.0 includes the following ready-made server controls:
Name |
Description |
Sqldatasource |
Supports binding to ADO. net providers (such as Microsoft? SQL Server, oledb, ODBC, or Oracle .) SQL database. |
Objectdatasource |
Supports binding to intermediate layer objects, such as data access layer or service components. |
Accessdatasource |
Support binding to Microsoft? Access (JET) database. |
Sitemapdatasource |
Supports binding to a level exposed by the ASP. NET 2.0 site navigation provider. |
Xmldatasource |
Supports binding to XML files or documents. |
Data Binding Control
The data binding control is a UI control that marks the data as the client device or browser that sends the request. The data binding control can automatically bind data exposed from the data source and obtain data at the appropriate time in the lifecycle of the page request. These controls can also choose to use data source functions, such as sorting, paging, filtering, updating, deleting, and inserting. The data binding control connects to the data source control through its performanceid attribute. You may be familiar with some data binding controls in ASP. NET 1.x, such as DataGrid, datalist, repeater, and list controls such as dropdownlist. ASP. NET 2.0 also contains several new data binding controls, such:
Name |
Description |
Gridview |
Displays data in a grid format. This control is an evolutionary form of the DataGrid Control and can automatically use the data source function. |
Detailsview |
Displays a single data item in a tag/value pair table, similar to Microsoft? The form view in access. This control can also automatically use the data source function. |
Formview |
A single data item is displayed once in a form defined by a custom template. Displays a single data item in a tag/value pair table, similar to Microsoft? The form view in access. This control can also automatically use the data source function. |
Treeview |
Data is displayed in the layered tree view of a scalable node. |
Menu |
Displays data in a hierarchical dynamic menu (including a pop-up menu. |
The following uses the gridview and sqldatasource controls to perform some simple data operations. I will give you a detailed example and give you a detailed explanation.
This is the simplest data driver page that only contains read-only reports. It displays data in the database, but does not allow us to change data items. First, we must set a sqldatasource control and connect to a database binding control. In this example, we use the gridview to specify the performanceid attribute of the gridview as the above sqldatasource.
The authors table of the pubs database in SQL Server 2000. Its structure is as follows:
The connectionstring attribute of the sqldatasource control specifies a database connection string. The selectcommand attribute specifies the database command to be searched. The connection string can be directly specified in the page code, or a new expression structure can be used to retrieve the database connection string in Web. config.
Step 1: drag a "gridview" control under "data" of "toolbox" to the home page, place the mouse on the Control for a short stay, A small label will appear:
Step 2: Open the drop-down list of "Select data source" shown in the following figure:
Step 3: If you have not set any sqldatasource, you can select the "new data source" option. After the interface is shown as follows:
We chose "Database" as our data source. An sqldatasource data source control with the ID of "sqlcece1" is automatically generated.
Step 4: When a sqldatasource data source control is generated on the home page, the following window is displayed:
Click "New Connection". The displayed form is as follows:
"Server name" is an instance on your local SQL Server. After entering this information, you can select two logon methods:
Windows Authentication and SQL Server Authentication. I will explain their differences in detail later. Here, we first select the Windows verification method. After selecting "pubs" database, click "test connection". If the connection is displayed successfully, it indicates that we have connected to the SQL Server server.
Step 5: click "OK". The "configure data source" form is displayed. We select "hoowoo. Pubs. DBO" as the database to be connected.
Step 6: because we want to save the connection string to the application configuration file, we want to keep the connection string in Web. config. Here we want to save the connection string as an alias: pubs
Step 7: select the authors table to select the data we need. Here we select all fields.
After completing the preceding steps, right-click the page and choose "view code". The following code is displayed:
<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
<title>GridView Bound Fields</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1"AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundField HeaderText="ID" DataField="au_id" ReadOnly="true" />
<asp:BoundField HeaderText="Last Name" DataField="au_lname" />
<asp:BoundField HeaderText="First Name" DataField="au_fname" />
<asp:BoundField HeaderText="Phone" DataField="phone" />
<asp:BoundField HeaderText="Address" DataField="address" />
<asp:BoundField HeaderText="City" DataField="city" />
<asp:BoundField HeaderText="State" DataField="state" />
<asp:BoundField HeaderText="Zip Code" DataField="zip" />
<asp:CheckBoxField HeaderText="Contract" DataField="contract" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone],[address],[city], [state], [zip], [contract] FROM [authors]" ConnectionString="<%$ ConnectionStrings:Pubs %>" />
</form>
</body>
</html>
The code in Web. config is as follows:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>
<connectionStrings>
<add name="Pubs" connectionString="Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="false"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
Now let's focus on the significance of these codes:
"<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" runat="server">"
The data binding control connects to the data source control through its performanceid attribute, so that we can perform a series of operations such as sorting, paging, filtering, updating, deleting, and inserting.
"<Columns>
<asp:BoundField HeaderText="ID" DataField="au_id" ReadOnly="true" />
<asp:BoundField HeaderText="Last Name" DataField="au_lname" />
<asp:BoundField HeaderText="First Name" DataField="au_fname" />
<asp:BoundField HeaderText="Phone" DataField="phone" />
<asp:BoundField HeaderText="Address" DataField="address" />
<asp:BoundField HeaderText="City" DataField="city" />
<asp:BoundField HeaderText="State" DataField="state" />
<asp:BoundField HeaderText="Zip Code" DataField="zip" />
<asp:CheckBoxField HeaderText="Contract" DataField="contract" />
</Columns>"
"Boundfield" and "checkboxfield" are both control types to be bound, and "headertext" is the name of the field to be displayed on the table, "datafield" is the data field to be bound.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [au_id], [au_lname], [au_fname],
[phone],[address],[city], [state], [zip], [contract] FROM [authors]"
ConnectionString="<%$ ConnectionStrings:Pubs %>" />
In the sqldatasource control, we set the database selectcommand to "select [au_id], [au_lname], [au_fname], [PHONE], [address] [City], [State], [Zip], [contract] from [authors] "exactly corresponds to the control to be bound to the gridview, which fully demonstrates the close relationship between the data binding control and the data source control.
Careful readers may be surprised. connectionstring = "<% $ connectionstrings: pubs %> what does connectionstring represent in sqldatasource? This issue is highly related to the Web. config configuration file. The following nodes are set in Web. config:
<connectionStrings>
<add name="Pubs" connectionString="Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
We can retrieve the Web. config configuration file to obtain the database connection string alias "pubs". The true meaning is
"Data Source = hoowoo; initial catalog = pubs; Integrated Security = true" providername = "system. Data. sqlclient" Initial catalog indicates that we are using the "pubs" database. Integrated Security indicates that Windows authentication is used.
The final display is as follows: