HTML <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" Font-Size="12px" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField> <EditItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </EditItemTemplate> <HeaderTemplate> <asp:CheckBox ID="CheckBox2" runat="server" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="产品编号" > <HeaderTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="pid" >产品编号<asp:Image ID="Image0" runat="server" ImageUrl="~/img/asc.gif" Visible="false" /></asp:LinkButton> </HeaderTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="产品名称" > <HeaderTemplate> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="pn" >产品名称<asp:Image ID="Image1" runat="server" ImageUrl="~/img/asc.gif" Visible="false" /></asp:LinkButton> </HeaderTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="总 价" > <HeaderTemplate> <asp:LinkButton ID="LinkButton3" runat="server" CommandName="pp">总 价<asp:Image ID="Image2" runat="server" ImageUrl="~/img/asc.gif" /></asp:LinkButton> </HeaderTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <EditRowStyle BackColor="#999999" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView> </div> </form> </body> </html> .cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections; using System.Collections.Generic; public class Product { private int _productId; public int ProductId { get { return _productId; } set { _productId = value; } } private string _productName; public string ProductName { get { return _productName; } set { _productName = value; } } private decimal _unitprice; public decimal Unitprice { get { return _unitprice; } set { _unitprice = value; } } } public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ViewState["sortDirection"] = "asc"; Bind(""); } } protected IList<Product> getList(string Where) { IList < Product > list= new List<Product>(); using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = "Data Source=.;Initial Catalog=NorthWind;Integrated Security=True;User ID=sa;pwd=sa;"; using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandText = "select productId,productName,unitprice from products "+Where; conn.Open(); using(SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while(sdr.Read()) { Product product = new Product(); product.ProductId=sdr.GetInt32(0); product.ProductName=sdr.GetString(1); product.Unitprice=sdr.GetDecimal(2); list.Add(product); } } } } return list; } protected void Bind(string where) { IList<Product> list = getList(where); if (list != null) { this.GridView1.DataSource = list; this.GridView1.DataBind(); } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { string orderStr = ""; if (ViewState["sortDirection"].ToString() == "asc") ViewState["sortDirection"] = "desc"; else ViewState["sortDirection"] = "asc"; switch (e.CommandName) { case "pid": orderStr = "productId"; break; case "pn": orderStr = "productName"; break; case "pp": orderStr = "unitprice"; break; } Bind(" order by " + orderStr + " " + ViewState["sortDirection"].ToString()); switch (e.CommandName) { case "pid": ((Image)this.GridView1.HeaderRow.FindControl("Image0")).ImageUrl = "img/" + ViewState["sortDirection"] + ".gif"; ((Image)this.GridView1.HeaderRow.FindControl("Image0")).Visible = true; ((Image)this.GridView1.HeaderRow.FindControl("Image1")).Visible = false; ((Image)this.GridView1.HeaderRow.FindControl("Image2")).Visible = false; break; case "pn": ((Image)this.GridView1.HeaderRow.FindControl("Image1")).ImageUrl = "img/" + ViewState["sortDirection"] + ".gif"; ((Image)this.GridView1.HeaderRow.FindControl("Image0")).Visible = false; ((Image)this.GridView1.HeaderRow.FindControl("Image1")).Visible = true; ((Image)this.GridView1.HeaderRow.FindControl("Image2")).Visible = false; break; case "pp": ((Image)this.GridView1.HeaderRow.FindControl("Image2")).ImageUrl = "img/" + ViewState["sortDirection"] + ".gif"; ((Image)this.GridView1.HeaderRow.FindControl("Image0")).Visible = false; ((Image)this.GridView1.HeaderRow.FindControl("Image1")).Visible = false; ((Image)this.GridView1.HeaderRow.FindControl("Image2")).Visible = true; break; } } }