asp.net| control in ASP.net 2.0, added a lot of new features and controls, compared to ASP.net 1.0/1.1, in all aspects have been greatly improved. Among them, in the data control aspect, has added many controls, in which the GridView control function is very formidable. In this article, a step-by-step example is given to the reader about the simple use of the GridView control in Asp.ne 2.0.
With ASP.net 1.0/1.1 's friends or feel, the DataGrid control function is very powerful and practical, but the attendant problem is that the feeling is still not easy to operate, For example, to use ado.net write data connection, binding DataGrid, Edit, delete, new data, etc. need a lot of code to achieve. In ASP.net 2.0, the DataGrid is still supported, but the new GridView control is more appealing, and the functionality is no less than the DataGrid, and it is easier to write code. Let's take a look at the GridView control with Visual Studio Express in the beta, and you can find it in the Data control page of the Toolbox, as shown in the following figure:
First, we create a new site with Visual Studio Express 1, and then drag the SqlDataSource control in the toolbar to the window. SqlDataSource is a data source control that makes it easy to configure a data source, establish a data connection, and almost no need to write code. We use the Northwind database in SQL SERVER 2000, so we can select the "Configure DataSource" on the right when we drag the control, as shown in the following figure:
After that, click the New button to create a data connection. In this article, we chose to localhost the local server, use Windows Integrated authentication, and choose to use the Northwind database, then click the Test Connection button to test the success of the connection, the success of the point "OK" completed, the following figure
Next, you will see a window showing details about the data connection in the following figure, click Next to Next
Then, in the pop-up window, ask if you want to save the data connection string into the Web.config file, and if you choose to save it, you just need to refer to its name every time you use the data connection string, and in this article we'll name it northwindconn. In fact, when we look at the Web.config file, we 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, point "next" to the next step,
At this point, you can select the columns to display, as shown in the following figure:
Point "Next" to the next step, you can see the system for you automatically generated SQL statements, the last point "finish" to end the operation.
In this way, the data connection has been established, we can switch to code view, see just the code created by the system below, where note the reference format for ConnectionString.
<asp:sqldatasource id= "SqlDataSource1"
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 and the SqlDataSource Control. Drag a GridView control into the design window, select SqlDataSource1 as its data source, and select all three selection boxes, such as the Enable paging,enable sorting,enable selection, Then we can see the effect immediately, as shown in the following figure
Finally, run the program, you can see the effect of the operation.
Next, we learn how to edit and delete data. Now we're going to use the UpdateCommand and DeleteCommand two properties to specify the SQL statements to use to update the data and delete the data, and to modify the SqlDataSource code 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 the products WHERE productid= @ProductID" >
</asp:SqlDataSource>
Run the program, the effect of the following figure:
Finally, let's look at a data control called Detailviews, which is similar to the use of the GridView control, but only one record at a time. Drag the Detailviews control from the toolbar to the design form, set its data source to SqlDataSource1, and select its paging selection box, as shown in the following figure:
And how do you insert a new record into the GridView? In Beta 1, the GridView temporarily does not offer automatic additions, but it can be done in other ways, such as using the InsertCommand attribute in SqlDataSource code 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 the products WHERE productid= @ProductID"
Insertcommand= INSERT into the products (ProductName, SupplierID, CategoryID,
QuantityPerUnit, UnitPrice) VALUES (@ProductName, @SupplierID, @CategoryID,
@QuantityPerUnit, CONVERT (Money, @UnitPrice)) "
</asp:SqlDataSource>
When the above code is complete, the Detailviews control's automatic IntelliSense prompt displays the Select box for the Enable inserting, which only needs to be checked to add new records, and the effect is as follows:
This article briefly introduces the GridView control in ASP.net 2.0 and its basic usage, and believes that the GridView control will be improved in the official version of Vs.net 2005.
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