In this series of articles, we built a simplified online shopping cart and PayPal system based on ASP.net 2.0, and through this example, the new GridView control introduced in ASP.net 2.0 is studied in depth.
So far, I've used the DataGrid control in almost every commercial c#.net project I've ever participated in; So, when I first heard about how the GridView was improving productivity, I tried it out very quickly. Both the DataGrid and the GridView are the new control classes provided in ASP.net 2.0 that allow you to quickly and easily display tabular data, and they can be converted into client-side HTML tables for display when viewed online.
I. INTRODUCTION
This is the first article in a series of articles. In this article, we'll focus on some of the uses of the GridView control through a simple online store sample program. Note that in each article we use the same source file. To see what this example demonstrates, you only need to extract the download to a new directory on your Web server and browse to the directory name. For example, if you extract all the content into a directory "Gridviewshop" under the root directory of your Web server, and navigate to this directory:
Http://www.yourserver.com/gridviewshop
If all goes well, you should see a site as shown in Figure 1 below:
Figure 1: This series of articles online store demo snapshot
Two. The GridView
If you've already implemented your system with a DataGrid, including your own custom paging and sorting schemes, you really don't need to consider updating to the GridView, because from a terminal effect, they all generate the same content (both generate an HTML table). However, if you are just beginning to develop a new system, then I suggest you use the GridView, especially if you want to take advantage of its built-in paging and sorting capabilities.
By setting various properties at design time, you can control the GridView from appearance to functionality. Later in this series, we'll explore these aspects in more depth by assigning some CSS classes to table rows and table column headers, and of course adding event handlers to allow users to interact with each row of data.
The fill GridView is similar to populating a DataGrid. You simply create the DataSource and then bind it to the GridView using the following code:
Mygridview.datasource = Yourdatasource;
Mygridview.databind ();
Of course, with. NET 2.0, you have another option, which is to create a SqlDataSource and bind the GridView directly to it. This is done by setting its DataSourceID to match the ID you assigned to SqlDataSource, that is:
<!--使用mySqlDataSource的ID创建SqlDataSource-->
<asp:SqlDataSource
id="mySqlDataSource"
runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT LastName FROM Employees">
</asp:SqlDataSource>
<!--创建GridView并且指派它的DataSourceID以匹配上面的mySqlDataSource-->
<asp:GridView
id="myGridView"
runat="server"
autogeneratecolumns="true"
DataSourceID="mySqlDataSource"/>
As an individual, I don't value this approach much, although it's a recommended way for Microsoft to build your gridview. I prefer to have more control over my datasource because, so I can manually filter its content or even more, which is why I do not use this method in the store demo program.
OK, let's continue with the discussion of building the store demo in this article. The general situation is that there are two gridviews on one page; you've seen this image before. A GridView is used to display the product of our store, while the other content corresponds to the shopping basket.
You can easily split these two parts into their respective pages, but for the sake of simplicity, we put them together.
If you open default.aspx (which is included in the corresponding zip source file in this article), you can see how the page was built.
Most HTML is used only for wrapping purposes; Note the declaration at the top of the page and the main
label and the label located inside it.
Three. Page declaration
<%@ page inherits= "Shop.site" src= "Cs/site.aspx.cs"%>
The page declaration simply tells us what namespace and class the page belongs to. In this example, our namespace is "shop" and our class is "site." There is also an additional attribute definition called "src" that points to an ordinary. cs text file that contains the site class.
I usually put my classes in an external. cs file during development and manually compile them into a. dll file. When I was working with Visual Studio, I was always accustomed to using precompiled DLLs during development, because later, you just needed a simple build to build them. Once I've done my job, I'll build the class into a precompiled DLL, but during development I prefer to spend more time coding rather than compiling.