gridview自定义分页

作者在 2009-07-14 17:45:05 发布以下内容
前台代码:
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dataGridPage.aspx.cs" Inherits="dataGridPage" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:DataGrid ID="DataGrid1" runat="server" PageSize="5">
    </asp:DataGrid>
    <asp:Button ID="Button1" runat="server" CommandName="上一页" Text="上一页" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button2" runat="server" CommandName="下一页" Text="下一页"
        onclick="Button2_Click" />
    </form>
</body>
</html>
 
后台代码:
   using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class dataGridPage : System.Web.UI.Page
{
    private int StartIndex;
    private SqlConnection MyConn = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Button1.Click += new System.EventHandler(this.GotoPage);
        this.Button2.Click += new System.EventHandler(this.GotoPage);
        if (!IsPostBack)
        {
            StartIndex = 0;
            MyConn = new SqlConnection("server = wxq; uid = sa; pwd = ; database = AES000datEnterprise");
            SqlCommand MyComm = new SqlCommand("select RecordTotal = count(*) from ApplicationOY", MyConn);
            MyConn.Open();
            SqlDataReader dr = MyComm.ExecuteReader(CommandBehavior.SingleRow);
            if (dr.Read())
            {
                DataGrid1.VirtualItemCount = (int)dr["RecordTotal"];
            }
            dr.Close();
            MyConn.Close();
            BindGridToSource(StartIndex, "上一页");
        }
    }
  
    private void GotoPage(object sender, System.EventArgs e)
    {
        int a =Convert.ToInt32(Math.Ceiling((double)(DataGrid1.VirtualItemCount / DataGrid1.PageSize)));
        string PageInfo = ((Button)sender).CommandName;
        switch (PageInfo)
        {
            case "上一页":
                if (DataGrid1.CurrentPageIndex > 0)
                    DataGrid1.CurrentPageIndex -= 1;
                break;
            case "下一页":
                if (((DataGrid1.CurrentPageIndex) ) < a-1)
                    DataGrid1.CurrentPageIndex += 1;
                else
                {
                    Button2.Enabled = false;
                    return;
                }
                break;
        }
        StartIndex = DataGrid1.CurrentPageIndex * DataGrid1.PageSize;
        BindGridToSource(StartIndex, PageInfo);
    }
    private void BindGridToSource(int StartPosition, string GoPage)
  {
      MyConn = new SqlConnection("server = wxq; uid = sa; pwd = ; database = AES000datEnterprise");
   SqlCommand MyComm = null;
   switch(GoPage)
   {
    case "上一页":
           Button2.Enabled = true;
           MyComm = new SqlCommand("select top 5 * from ApplicationOY " +
      "where ID >= @uid order by ID",MyConn);
     if (StartPosition == 0)
      MyComm.Parameters.Add("@uid",SqlDbType.Int).Value = 0;
     else
      MyComm.Parameters.Add("@uid",SqlDbType.NVarChar,5).Value =
       ViewState[(DataGrid1.CurrentPageIndex+1).ToString()];
     break;
    case "下一页":
     MyComm = new SqlCommand("select top 5 * from ApplicationOY " +
      "where ID > @uid order by ID", MyConn);
     MyComm.Parameters.Add("@uid",SqlDbType.NVarChar,
      18).Value = DataGrid1.Items[4].Cells[0].Text;
     break;
   }
   MyConn.Open();
   SqlDataReader dr = MyComm.ExecuteReader();
   DataGrid1.DataSource = dr;
   DataGrid1.DataBind();
   dr.Close();
   MyConn.Close();
   ViewState[(DataGrid1.CurrentPageIndex + 1).ToString()] =
    DataGrid1.Items[0].Cells[0].Text;
  }
 
 
    protected void Button2_Click(object sender, EventArgs e)
    {
     
    }
}
默认分类 | 阅读 4899 次
文章评论,共0条
游客请输入验证码
浏览4899次
文章分类
文章归档
最新评论