Use the GridView control in ASP.NET 2.0

xiaoxiao2021-03-06  115

In ASP.NET 2.0, many new features and controls have been added, compared to ASP.NET 1.0 / 1.1, it has greatly improved in all respects. Among them, in terms of data control, there is a lot of controls, where the GridView control function is very powerful. In this article, a simple use of the GridView control in ASP.NE 2.0 will be described in an actual example.

Users who have used ASP.NET 1.0 / 1.0 or also feel that the DataGrid control function is very powerful and practical, but the problem with that feels is still unimplete, such as using ADO. NET write data connection, bind DataGrid, editing, deletion, new data, etc. must be achieved. In ASP.NET 2.0, the DataGrid is still supported, but the new GridView control is more attractive, and the function is not inferior to DataGrid, more convenient, and written code. Let's take a look at the GridView control in Visual Studio Express 2005 Beta 1, you can find it in the toolbox's DATA control page, as shown below:

First, we create a new site with Visual Studio Express 2005 Beta 1, then drag the SqlDataSource control in the toolbar to the window. SqlDataSource is a data source control, which uses it easy to configure the data source, establishing a data connection, almost no writing code. We use the Northwind database in SQL Server 2000, so we can click "Configure Datasource" that appears on the right, as shown below in dragging the control.

After that, click the New button to create a new data connection. In this article, we select the localhost local server, use Windows integration authentication, and choose to use the Northwind database, then click the Test Connection button to test whether the connection is successful, success, click "OK", as shown below

At this point, you can choose the column to display, as shown below:

Click "Next" to the next step, you can see the system to generate the SQL statement you automatically, and finally "finish" ends the operation.

In this way, the data has been established, we can switch to the code view, see the code created by the system, which is as follows, where you pay attention to the reference format of Connectionstring.

Run the program, the effect is as follows:

Finally, let's take another data control called DetailViews, which is similar to the usage of the GridView control, but not, only one record is displayed each time. Drag the DetailViews control in the toolbar to the design form and set its data source as SqlDataSource1, and select the selection box for their paging, as shown below:

How to insert a new record into GridView? In Beta 1, GridView does not provide automatic increased features, but can be implemented by other methods, such as using the InsertCommand property in the SqlDataSource code, the code is as follows:

After completing the above code, the automatic intelligence sensing prompt of the DetailViews control will display the selection box of Enable Inserting, and you can add a record in the selection box. The effect is as follows :

This article briefly describes the GridView control in ASP.NET 2.0 and its basic usage. I believe that GridView controls will have greater improvements in the official version of VS.NET 2005.

Next, you will see the display of the display of the data connection details as shown below, click Next to the next step.

After that, in the pop-up window, ask if the data connection string is saved in the web.config file, if you choose to save, each time you use the data connection string, you only need to reference its name, in this article, we Name it northwindconn. In fact, we will find the web.config file at this time, it will find the following code:

... then click" Next "to the next step,

转载请注明原文地址:https://www.9cbs.com/read-99142.html

New Post(0)