Gridview中排序方法二

前台代码:

View Code
1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
2 <Columns>
3 <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
4 <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
5 <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
6 <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
7 </Columns>
8 </asp:GridView>
9 <asp:Label ID="Label1" runat="server" Text="当前页"></asp:Label>&nbsp;
10 <asp:Label ID="Label2" runat="server" Text="1"></asp:Label>&nbsp;
11 <asp:Label ID="Label3" runat="server" Text="总页数"></asp:Label>&nbsp;
12 <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>&nbsp;
13 <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">首页</asp:LinkButton>&nbsp;
14 <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">上一页</asp:LinkButton>&nbsp;
15 <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">下一页</asp:LinkButton>&nbsp;
16 <asp:LinkButton ID="LinkButton4" runat="server" onclick="LinkButton4_Click">尾页</asp:LinkButton>

后台代码:

View Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 databind();
6 }
7
8 }
9 public void databind()
10 {
11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
12 SqlCommand cmd = new SqlCommand("SELECT * FROM CUSTOMERS", con);
13 SqlDataAdapter da = new SqlDataAdapter(cmd);
14 DataSet ds = new DataSet();
15 da.Fill(ds);
16 PagedDataSource ps = new PagedDataSource();
17 ps.DataSource = ds.Tables[0].DefaultView;
18 ps.AllowPaging = true;
19 ps.CurrentPageIndex = int.Parse(this.Label2.Text) - 1;
20 this.Label4.Text = ps.PageCount.ToString();
21 this.LinkButton1.Enabled = true;
22 this.LinkButton2.Enabled = true;
23 this.LinkButton3.Enabled = true;
24 this.LinkButton4.Enabled = true;
25 if (this.Label2.Text == "1")
26 {
27 this.LinkButton1.Enabled = false;
28 this.LinkButton2.Enabled = false;
29 }
30 if (this.Label2.Text == this.Label4.Text)
31 {
32 this.LinkButton3.Enabled = false;
33 this.LinkButton4.Enabled = false;
34 }
35 this.GridView1.DataSource = ps;
36 this.GridView1.DataBind();
37 }
38 protected void LinkButton1_Click(object sender, EventArgs e)
39 {
40 this.Label2.Text = "1";
41 databind();
42 }
43 protected void LinkButton2_Click(object sender, EventArgs e)
44 {
45 this.Label2.Text = Convert.ToString( Convert.ToInt32(this.Label2.Text) - 1);
46 databind();
47 }
48 protected void LinkButton3_Click(object sender, EventArgs e)
49 {
50 this.Label2.Text = Convert.ToString( Convert.ToInt32(this.Label2.Text) + 1);
51 databind();
52 }
53 protected void LinkButton4_Click(object sender, EventArgs e)
54 {
55 this.Label2.Text = this.Label4.Text;
56 databind();
57 }

怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
原文地址:https://www.cnblogs.com/hfliyi/p/1982698.html