<div>
<table>
<tr>
<td align="center" style="height: 25px;font-size:12px">
<asp:HiddenField ID="CurrentPage" runat="server" Value="1"/>
<asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
<asp:Label ID="LblPageCount" runat="server"></asp:Label>
<asp:Label ID="LblRecordCount" runat="server"></asp:Label>
<asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
runat="server">首 页</asp:LinkButton>
<asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页 </asp:LinkButton>
<asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
转到第<asp:DropDownList runat="server" ID="ddlPageNumbers" Width="40px" AutoPostBack="true" OnSelectedIndexChanged="ddlPageNumbers_SelectedIndexChanged"></asp:DropDownList>页
</td>
</tr>
</table>
</div>
后台
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;
public partial class BD_UserControl_SmartPageUC : System.Web.UI.UserControl
{
#region 定义属性
//表名
public string tableName
{
get { return (string)ViewState["tableName"]; }
set { ViewState["tableName"] = value; }
}
//字段名
public string fldName
{
get { return (string)ViewState["fldName"]; }
set { ViewState["fldName"] = value; }
}
//CheckBox标识是否被选择
public ArrayList SelectDeal
{
get { return (ArrayList)ViewState["SelectDeal"]; }
set { ViewState["SelectDeal"] = value; }
}
//查询条件
public string MaterielSelect
{
get { return (string)ViewState["MaterielSelect"]; }
set { ViewState["MaterielSelect"] = value; }
}
//存储过程名字
public string ProcName
{
get { return (string)ViewState["ProcName"]; }
set { ViewState["ProcName"] = value; }
}
//存储过程名字
public string SqlStr
{
get { return (string)ViewState["SqlStr"]; }
set { ViewState["SqlStr"] = value; }
}
#endregion
#region 页面加载及变量的定义
FH_ERP.PublicBusiness DSNs = new FH_ERP.PublicBusiness();
DataSet dsTemp = new DataSet();
int pageSize;
int totals;
int pageIndex;
private int newIndex;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dsTemp = this.Bind(tableName, fldName, MaterielSelect, "", SqlStr);
}
}
#endregion
#region GridView的绑定,只用于首次加载,只加载前N条记录
public DataSet Bind(string tableName, string fldName, string strWhere, string PageIndex, string SqlStr)
{
#region 只加载前SetupList.PageSize条记录
GridView SetupList = (GridView)this.Parent.FindControl("SetupList");
pageSize = 0;
totals = 0;
pageIndex = 0;
newIndex = 0;
DataSet dsCount = new DataSet();
if (SqlStr != null)
{
if (MaterielSelect == null)
{
dsCount = DSNs.GetData(SqlStr);
}
else
{
dsCount = DSNs.GetData(SqlStr + " where " + MaterielSelect);
}
int pc = Convert.ToInt32(dsCount.Tables[0].Rows.Count.ToString());
int page = (pc % SetupList.PageSize) == 0 ? (pc / SetupList.PageSize) : (pc / SetupList.PageSize + 1);
//str = str.Replace("'", "''");
if (MaterielSelect == null)
{
dsTemp = DSNs.GetPageList(tableName, fldName, SetupList.PageSize, 1, "", ProcName);
}
else
{
dsTemp = DSNs.GetPageList(tableName, fldName, SetupList.PageSize, 1, MaterielSelect, ProcName);
}
SetupList.DataSource = dsTemp;
SetupList.DataKeyNames = new string[] { fldName };
SetupList.DataBind();
#region 控件判断
LblCurrentIndex.Text = "第 " + (SetupList.PageIndex + 1).ToString() + " 页";
LblPageCount.Text = "共 " + page + " 页";
LblRecordCount.Text = "总共 " + pc + " 条";
ddlPageNumbers.Items.Clear();
for (int i = 0; i < page; i++)
{
ddlPageNumbers.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
}
ddlPageNumbers.SelectedValue = (SetupList.PageIndex + 1).ToString();
CurrentPage.Value = "1";
LblCurrentIndex.Text = "第 " + CurrentPage.Value + " 页";
ddlPageNumbers.SelectedValue = CurrentPage.Value;
if (dsTemp.Tables[0].Rows.Count == 0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;
}
else if (ddlPageNumbers.Items.Count == 1)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else if (int.Parse(CurrentPage.Value) == 1)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
else if (int.Parse(CurrentPage.Value) == ddlPageNumbers.Items.Count)
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
#endregion
}
return dsTemp;
#endregion
}
#endregion
#region 控件的显示
public void ValEnabled()
{
LblCurrentIndex.Text = "第 " + CurrentPage.Value + " 页";
ddlPageNumbers.SelectedValue = CurrentPage.Value;
if (dsTemp.Tables[0].Rows.Count == 0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;
}
else if (ddlPageNumbers.Items.Count == 1)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else if (int.Parse(CurrentPage.Value) == 1)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
else if (int.Parse(CurrentPage.Value) == ddlPageNumbers.Items.Count)
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
}
#endregion
#region 首页,上一页,下一页,尾页 的点击事件
protected void PagerButtonClick(object sender, EventArgs e)
{
GridView SetupList = (GridView)this.Parent.FindControl("SetupList");
pageSize = SetupList.PageSize;
pageIndex = Convert.ToInt32(CurrentPage.Value);
newIndex = pageIndex;
//LblRecordCount.Text.Substring(3, 2);
totals = int.Parse(LblRecordCount.Text.Substring(3, LblRecordCount.Text.Length - 4));
int pages = (totals % pageSize) == 0 ? (totals / pageSize) : (totals / pageSize + 1);
string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
switch (arg)
{
case "prev":
if (pageIndex > 0)
{
pageIndex -= 1;
}
break;
case "next":
if (pageIndex < pages)
{
pageIndex += 1;
}
break;
case "last":
pageIndex = pages;
break;
default:
pageIndex = 1;
break;
}
CurrentPage.Value = pageIndex.ToString();
//AddDeleteDealToViewState();
if (MaterielSelect == null)
{
dsTemp = DSNs.GetPageList(tableName, fldName, pageSize, pageIndex, "", ProcName);
}
else
{
dsTemp = DSNs.GetPageList(tableName, fldName, pageSize, pageIndex, MaterielSelect, ProcName);
}
SetupList.DataSource = dsTemp;
SetupList.DataBind();
ValEnabled();
}
#endregion
#region DropDownList的点击事件,用于选择页码
protected void ddlPageNumbers_SelectedIndexChanged(object sender, EventArgs e)
{
GridView SetupList = (GridView)this.Parent.FindControl("SetupList");
pageIndex = int.Parse(ddlPageNumbers.SelectedValue);
pageSize = SetupList.PageSize;
CurrentPage.Value = pageIndex.ToString();
//AddDeleteDealToViewState();
if (MaterielSelect == null)
{
dsTemp = DSNs.GetPageList(tableName, fldName, pageSize, pageIndex, "", ProcName);
}
else
{
dsTemp = DSNs.GetPageList(tableName, fldName, pageSize, pageIndex, MaterielSelect, ProcName);
}
SetupList.DataSource = dsTemp;
SetupList.DataBind();
ValEnabled();
}
#endregion
#region 添加删除是否选中的状态,用于删除(不用了)
/ <summary>
/ make a ViewState for save Id which setup you want to delete
/ </summary>
//private void AddDeleteDealToViewState()
//{
// GridView SetupList = (GridView)this.Parent.FindControl("SetupList");
// ArrayList al;
// if (ViewState["SelectDeal"] == null)
// {
// al = new ArrayList();
// ViewState.Add("SelectDeal", al);
// }
// else
// {
// al = (ArrayList)ViewState["SelectDeal"];
// }
// CheckBox cb;
// foreach (GridViewRow gvr in SetupList.Rows)
// {
// //cb = (CheckBox)gvr.Cells[0].FindControl("DeleteCheckBox");
// cb = (CheckBox)gvr.Cells[0].Parent.FindControl("DeleteCheckBox");
// if (cb.Checked)
// {
// if (!al.Contains(Convert.ToInt32(SetupList.DataKeys[gvr.RowIndex][0])))
// al.Add(Convert.ToInt32(SetupList.DataKeys[gvr.RowIndex][0]));
// }
// else
// {
// if (al.Contains(Convert.ToInt32(SetupList.DataKeys[gvr.RowIndex][0])))
// al.Remove(Convert.ToInt32(SetupList.DataKeys[gvr.RowIndex][0]));
// }
// }
//}
#endregion
}