036. asp.netWeb用户控件之五使用用户控件实现分页数据导航
UserDataPager.ascx用户控件代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserDataPager.ascx.cs" Inherits="UserDataPager" %> <style type="text/css"> .style8 { } .style11 { width: 249px; font-size: small; } </style> <table cellpadding="0" cellspacing="0" style="border: 1px solid #999999; font-size: small; height: 25px; width: 403px;" bgcolor="#F2F2F2"> <tr> <td style="text-align: center" class="style11"> 共有数据<asp:Label ID="lbldatanum" runat="server" ForeColor="Red"></asp:Label> 条 每页<asp:Label ID="lblnum" runat="server" ForeColor="Red"></asp:Label> 条 第<asp:Label ID="lblCurrentPage" runat="server" ForeColor="Red"></asp:Label> 页/共<asp:Label ID="lblcount" runat="server" Font-Size="9pt" ForeColor="Red"></asp:Label> 页</td> <td style="text-align: center" class="style8"> <asp:LinkButton ID="lbtnFirst" runat="server" Font-Size="9pt" onclick="lbtnFirst_Click" Enabled="False">|<</asp:LinkButton> <asp:LinkButton ID="lbtnForward" runat="server" Font-Size="9pt" onclick="lbtnForward_Click" Enabled="False"><</asp:LinkButton> <asp:LinkButton ID="lbtnBackwards" runat="server" Font-Size="9pt" onclick="lbtnBackwards_Click">></asp:LinkButton> <asp:LinkButton ID="lbtnLast" runat="server" Font-Size="9pt" onclick="lbtnLast_Click">>|</asp:LinkButton> </td> </tr> </table>
UserDataPager.ascx.cs代码:
public partial class UserDataPager : System.Web.UI.UserControl { protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态 private SqlConnection conn; private object operateID; public object OperateID { get { return operateID; } set { operateID = value; } } private int pageNum=5; public int PageNum { get { return pageNum; } set { pageNum = value; } } private string strSQL = ""; public string StrSQL { get { return strSQL; } set { strSQL = value; } } private string sql = ""; public string DataSQL { get { return sql; } set { sql = value; } } private void BindDataList(int currentpage) { string mytype = operateID.GetType().ToString(); mytype = mytype.Substring(mytype.LastIndexOf(".") + 1, mytype.Length - 1 - mytype.LastIndexOf(".")); conn = new SqlConnection(strSQL); pds.AllowPaging = true;//允许分页 pds.PageSize = pageNum;//每页显示3条数据 pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值 conn.Open();//打开数据库连接 SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); sda.Fill(ds);//把执行得到的数据放在数据集中 pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中 lblcount.Text = pds.PageCount.ToString(); lblCurrentPage.Text =(pds.CurrentPageIndex + 1).ToString(); lblnum.Text = pageNum.ToString(); lbldatanum.Text = ds.Tables[0].Rows.Count.ToString(); if (pds.PageCount > 1) { lbtnBackwards.Enabled = true; lbtnLast.Enabled = true; } else { lbtnBackwards.Enabled = false; lbtnLast.Enabled = false; } if (mytype == "GridView") { ((GridView)(operateID)).DataSource = pds; ((GridView)(operateID)).DataBind(); } else if (mytype == "DataList") { ((DataList)(operateID)).DataSource = pds; ((DataList)(operateID)).DataBind(); } conn.Close(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDataList(0); } } protected void lbtnFirst_Click(object sender, EventArgs e)//首页 { pds.CurrentPageIndex = 0; BindDataList(pds.CurrentPageIndex); lbtnFirst.Enabled = false; lbtnForward.Enabled = false; lbtnBackwards.Enabled = true; lbtnLast.Enabled = true; } protected void lbtnForward_Click(object sender, EventArgs e)//上一页 { if (pds.CurrentPageIndex >= 0) { pds.CurrentPageIndex = pds.CurrentPageIndex - 1; BindDataList(pds.CurrentPageIndex); lbtnBackwards.Enabled = true; lbtnLast.Enabled = true; if (pds.CurrentPageIndex == 0) { lbtnForward.Enabled = false; lbtnFirst.Enabled = false; } } } protected void lbtnBackwards_Click(object sender, EventArgs e)//下一页 { if (pds.CurrentPageIndex <= pds.PageCount - 1) { lbtnFirst.Enabled = true; lbtnForward.Enabled = true; pds.CurrentPageIndex = pds.CurrentPageIndex + 1; BindDataList(pds.CurrentPageIndex); if (pds.CurrentPageIndex == pds.PageCount - 1) { lbtnBackwards.Enabled = false; lbtnLast.Enabled = false; } } } protected void lbtnLast_Click(object sender, EventArgs e)//尾页 { pds.CurrentPageIndex = pds.PageCount - 1; BindDataList(pds.CurrentPageIndex); lbtnLast.Enabled = false; lbtnBackwards.Enabled = false; lbtnFirst.Enabled = true; lbtnForward.Enabled = true; } }
Default.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register src="UserDataPager.ascx" tagname="UserDataPager" tagprefix="uc1" %> <!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"> .style7 { width: 100%; height: 42px; } </style> </head> <body> <form id="form1" runat="server"> <div style="width: 1019px; text-align: center; height: 54px;"> <table align="center" cellpadding="0" cellspacing="0" class="style7"> <tr> <td style="text-align: left"> <uc1:UserDataPager ID="UserDataPager1" runat="server" /> </td> </tr> <tr> <td> </td> </tr> </table> </div> <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="Ridge" BorderWidth="1px" CellPadding="3" Width="403px"> <RowStyle ForeColor="#000066" /> <FooterStyle BackColor="White" ForeColor="#000066" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> </form> </body> </html>
Default.aspx.cs代码:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserDataPager1.OperateID = GridView1; //设置针对哪个控件进行分页描述
UserDataPager1.StrSQL = "server=.;uid=sa;pwd=123.456;database=TYW;";//设置链接字符串
UserDataPager1.DataSQL = "select * from card";
UserDataPager1.PageNum = 6;
}
}
最终效果: