Use the gridview control in ASP. NET 2.0

Source: Internet
Author: User
Tags connectionstrings
In ASP. NET 2.0, many new functions and controls are added. Compared with Asp.net 1.0/1.1, they are greatly improved in various aspects. In terms of data controls, many controls are added, and the gridview control is very powerful. In this article, we will introduce the simple use of the gridview control in ASP. Ne 2.0 step by step.

Friends who have used Asp.net 1.0/1.1 may also feel that the DataGrid control is very powerful and practical, but the problem that it is not easy to operate is that, for example, using ado.net to write data connections, binding DataGrid, editing, deleting, and adding data requires a lot of code. In Asp.net 2.0, the DataGrid is still supported, but the newly added gridview control is more attractive, and its function is no inferior to that of the DataGrid, making it more convenient and less written code. Let's take a look at the gridview control in Visual Studio express 2005 Beta 1. You can find it on the Data Control page of the toolbox, for example:

First, we use Visual Studio express 2005 Beta 1 to create a site, and then drag the sqldatasource control in the toolbar to the window. Sqldatasource is a data source control that allows you to easily configure data sources and establish data connections without writing code. We use the northwind database in SQL Server 2000, so we can click "Configure datasource" on the right when dragging the control, for example:

Click the new button to create a new data connection. In this article, we select the localhost local server, use Windows integrated authentication, and choose to use the northwind database, and then click the test connection button to test whether the connection is successful. If yes, click "OK", as shown in figure

Next, you will see the window showing detailed information about data connections, as shown in. Click Next to next.

Then, in the pop-up window, ask whether to save the data connection string to the Web. in the config file, if you choose to save, you only need to reference the data connection string every time you arrive. In this article, we name it northwindconn. In fact, when we view the Web. config file, we will find the following code:

<Configuration
Xmlns =
Http://schemas.microsoft.com/.NetConfiguration/v2.0>
<Appsettings/>
<Connectionstrings>
<Add name = "northwindconn"
Connectionstring = "Server = (local); Integrated Security = true;
Database = northwind; persist Security info = true"
Providername = "system. Data. sqlclient"/>
</Connectionstrings>...

Next, click "Next" to the next step,

You can select the columns to display, for example:

Click "Next" to the next step. The SQL statement automatically generated by the system is displayed. Click "finish" to complete the operation.

Now that the data connection has been established, we can switch to the Code view and view the code just created by the system as follows. Pay attention to the onstring reference format.

<Asp: sqldatasource id = "sqlperformance1"
Runat = "server"
Selectcommand = "select [productid], [productname], [supplierid],
[Categoryid], [quantityperunit], [unitprice] From
[Alphabetical list of products]"
Connectionstring = "<% $ connectionstrings: northwindconn %>">
</ASP: sqldatasource>

The second step is to bind the gridview control to the sqldatasource control. Drag a gridview control to the design window, select sqlperformance1 as its data source, and select enable paging, enable sorting, enable selection, and other three selection boxes, then we can see the effect immediately, as shown in figure

Finally, run the program to see the running effect.

Next, we will learn how to edit and delete data. The updatecommand and deletecommand attributes are used to specify the SQL statements used to update and delete data. The code for modifying sqldatasource is as follows:

<Asp: sqldatasource id = "sqldatasource1" runat = "server"
Selectcommand = "select [productid], [productname], [supplierid], [categoryid],
[Quantityperunit], [unitprice] from [alphabetical list of products]"
Connectionstring = "<% $ connectionstrings: northwindconn %>"
Updatecommand = "update products set productname = @ productname,
Supplierid = @ supplierid, categoryid = @ categoryid, quantityperunit = @ quantityperunit,
Unitprice = convert (money, @ unitprice) Where productid = @ productid"
Deletecommand = "delete from products where productid = @ productid">
</ASP: sqldatasource>

Run the program. The effect is as follows:

Finally, let's look at a data control called detailviews, which is similar to the usage of the gridview control, but not the same: Only one record is displayed at a time. Drag the detailviews control in the toolbar to the design form, set its data source to sqlperformance1, and select its paging selection box, for example:

How to insert a new record to the gridview? In Beta 1, gridview does not provide the auto-increment function for the time being, but can be implemented by other methods. For example, you can use the insertcommand attribute in sqldatasource Code. The Code is as follows:

<Asp: sqldatasource id = "sqldatasource1" runat = "server"
Selectcommand = "select [productid], [productname], [supplierid], [categoryid],
[Quantityperunit], [unitprice] from [products]"
Connectionstring = "<% $ connectionstrings: northwindconn %>"
Updatecommand = "update products set productname = @ productname,
Supplierid = @ supplierid, categoryid = @ categoryid,
Quantityperunit = @ quantityperunit, unitprice = convert (money, @ unitprice)
Where (productid = @ productid )"
Deletecommand = "delete from products where productid = @ productid"
Insertcommand = "insert into products (productname, supplierid, categoryid,
Quantityperunit, unitprice) values (@ productname, @ supplierid, @ categoryid,
@ Quantityperunit, convert (money, @ unitprice) ">
</ASP: sqldatasource>

After the above Code is completed, the automatic intelligent sensing prompt of the detailviews control will display the Enable inserting selection box. You only need to check this selection box to add a new record. The effect is as follows:

This article briefly introduces the gridview control in Asp.net 2.0 and its basic usage. I believe that the gridview control will be improved in the official version of vs.net 2005.

Source: http://www.webjx.com/htmldata/2005-03-11/1110510758.html

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.