Asp.Net数据分页技术全功略(2)

html页面代码:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageIndexTest.aspx.cs" Inherits="PagIndedexTest" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 61%;
            margin-right: 0px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:DataGrid ID="DataGrid1" runat="server" BackColor="White"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
            ForeColor="Black" GridLines="Horizontal" Width="616px"
            onpageindexchanged="DataGrid1_PageIndexChanged"
            onitemdatabound="DataGrid1_ItemDataBound">
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <SelectedItemStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
        </asp:DataGrid>
        <span lang="zh-cn">共</span><asp:Label ID="Lab_TotalPageSize" runat="server"
            Text="Label"></asp:Label>
        <span lang="zh-cn">页 当前是第</span><asp:Label ID="Lab_CurrentPageSize"
            runat="server" Text="Label"></asp:Label>
        <span lang="zh-cn">页</span><br />
        <table align="center" class="style1">
            <tr>
                <td>
                    <asp:HyperLink ID="Top" runat="server">首页</asp:HyperLink>
                </td>
                <td>
                    <asp:HyperLink ID="Next" runat="server">下一页</asp:HyperLink>
                </td>
                <td>
                    <asp:HyperLink ID="Prev" runat="server">上一页</asp:HyperLink>
                </td>
                <td>
                    <asp:HyperLink ID="Bottom" runat="server">未页</asp:HyperLink>
                </td>
            </tr>
        </table>
   
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </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;
using System.Web.Configuration;

public partial class PagIndedexTest : System.Web.UI.Page
{
    private static int PageTotalCount;
    private const int PageSize = 3;
    private int PageNo;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.DataGrid1.PageSize = PageSize;
            this.Lab_CurrentPageSize.Text = "1";
            this.GetTotalPageSize();
            this.Lab_TotalPageSize.Text = PageTotalCount.ToString();
            this.BindList();
        }
    }

    private DataSet GetDataSet(int startIndex, int MaxIndex)
    {
        string con = "server=.;database=xscj;uid=sa;pwd=yzc;pooling=true";
        string sql = "select * from xs";
        SqlConnection conection = new SqlConnection(con);
        SqlDataAdapter da = new SqlDataAdapter(sql, conection);
        conection.Open();
        DataSet DS = new DataSet();
        da.Fill(DS, startIndex, MaxIndex, "xs");
        conection.Close();
        return DS;
    }


    private void GetTotalPageSize()
    {
        string sql = "select count(*) from xs";
        string stringConn = WebConfigurationManager.ConnectionStrings["xscjConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(stringConn);
        con.Open();
        SqlCommand cmd = new SqlCommand(sql,con);
        int Count = Convert.ToInt32(cmd.ExecuteScalar());
        PageTotalCount = Convert.ToInt32(Count / PageSize);
        if (PageTotalCount * PageSize < Count)
        {
            PageTotalCount++;
        }
    }

    private void BindList()
    {
        if (Request.QueryString["requestedPage"] != null)
        {
            PageNo = System.Int32.Parse(Request.QueryString["requestedPage"]);
        }
        else
        {
            PageNo = 1;
        }
        this.Lab_CurrentPageSize.Text = this.PageNo.ToString();
        if (PageTotalCount == 1)
        {
            this.Top.Enabled = false;
            this.Next.Enabled = false;
            this.Prev.Enabled = false;
            this.Bottom.Enabled = false;
        }
        else if (PageNo == 1 & !(PageTotalCount == 1))
        {
            this.Top.Enabled = false;
            this.Next.Enabled = true;
            this.Prev.Enabled = false;
            this.Bottom.Enabled = true;
            this.Next.NavigateUrl = "PageIndexTest.aspx?&requestedPage=" + Convert.ToString(PageNo + 1) + "&TotalCount=" + PageTotalCount.ToString();
            this.Bottom.NavigateUrl = "PageIndexTest.aspx?&requestedPage=" + Convert.ToString(PageTotalCount) + "&TotalCount=" + PageTotalCount.ToString();
        }
        else if (!(PageNo == 1) & !(PageNo == PageTotalCount))
        {
            this.Top.Enabled = true;
            this.Next.Enabled = true;
            this.Prev.Enabled = true;
            this.Bottom.Enabled = true;
            this.Top.NavigateUrl = "PageIndexTest.aspx?&requestedPage=1" + "&TotalCount=" + PageTotalCount.ToString(); ;
            this.Bottom.NavigateUrl = "PageIndexTest.aspx?&requestedPage=" + PageTotalCount.ToString() + "&TotalCount=" + PageTotalCount.ToString();
            this.Prev.NavigateUrl = "PageIndexTest.aspx?&requestedPage=" + Convert.ToString(PageNo - 1) + "&TotalCount=" + PageTotalCount.ToString();
            this.Next.NavigateUrl = "PageIndexTest.aspx?&requestedPage=" + Convert.ToString(PageNo + 1) + "&TotalCount=" + PageTotalCount.ToString();
        }
        else if (PageNo == PageTotalCount)
        {
            this.Prev.Enabled = true;
            this.Top.Enabled = true;
            this.Top.NavigateUrl = "PageIndexTest.aspx?&requestedPage=1" + "&TotalCount=" + PageTotalCount.ToString(); ;
            this.Prev.NavigateUrl = "PageIndexTest.aspx?&requestedPage=" + Convert.ToString(PageNo - 1) + "&TotalCount=" + PageTotalCount.ToString();
            this.Prev.NavigateUrl = "PageIndexTest.aspx?&requestedPage=" + Convert.ToString(PageNo - 1) + "&TotalCount=" + PageTotalCount.ToString();

            this.Next.Enabled = false;
            this.Bottom.Enabled = false;
        }
        DataSet ds = GetDataSet((PageNo - 1) * PageSize, PageSize);
        this.DataGrid1.DataKeyField = "XH";
        this.DataGrid1.DataSource = ds;
        this.DataGrid1.DataBind();
    }
    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
        this.BindList();
    }
    protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            e.Item.Attributes.Add("onmouseover", "color=this.style.backgroundColor,this.style.backgroundColor='#E0E0E0'");
            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script>alert(\'sdfsdf\')</script>");
    }
}

posted on 2009-01-04 16:46  闫振创  阅读(352)  评论(0编辑  收藏  举报

导航