Asp.Net数据分页技术全功略(3)
html页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageSort1.aspx.cs" Inherits="PageSort1" %>
<!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">
<asp:DataGrid ID="DataGrid1" runat="server"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
CellPadding="4"
Width="531px" onpageindexchanged="DataGrid1_PageIndexChanged">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<SelectedItemStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left"
Mode="NumericPages" />
<ItemStyle BackColor="White" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:DataGrid>
<div>
<asp:Button ID="top" runat="server" onclick="top_Click" Text="Top" />
<asp:Button ID="next" runat="server" onclick="next_Click" Text="Next" />
<asp:Button ID="pre" runat="server" onclick="pre_Click" Text="Pre" />
<asp:Button ID="bottom" runat="server" onclick="bottom_Click" Text="Bottom" />
</div>
</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>");
}
}