-----------------------用户控件---------------
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="pageNum.ascx.cs" Inherits="WebBookShop.UserControls.pageNum" %>
<link href="Css/page_bottom_new.css" rel="stylesheet" type="text/css" />
第
<asp:Label ID="LabelNowPage" runat="server" Text="1"></asp:Label>
页 共
<asp:Label ID="LabelPageRow" runat="server" Text="1"></asp:Label>
页>
<asp:Button ID="PageUp" CssClass="anniu" runat="server" Text="上一页"
onclick="PageUp_Click" />
<asp:Button ID="PageDown" CssClass="anniu" runat="server" Text="下一页"
onclick="PageDown_Click" />
<asp:TextBox ID="TexToPage" CssClass="anniu" runat="server" Height="18px"
Width="37px" ValidationGroup="ToPage"></asp:TextBox>
<asp:Button ID="BtnToPage" CssClass="anniu" runat="server" Text="跳转"
onclick="BtnToPage_Click" ValidationGroup="ToPage" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TexToPage" ErrorMessage="RequiredFieldValidator"
ForeColor="Red" ValidationGroup="ToPage">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TexToPage" Display="Dynamic" ErrorMessage="CompareValidator"
ForeColor="Red" Operator="DataTypeCheck" Type="Integer"
ValidationGroup="ToPage">*</asp:CompareValidator>
------------------------------------前台代码调用
<%@ Register src="~/UserControls/pageNum.ascx" tagname="pageNum" tagprefix="uc1" %>
<dt class="ipt2">
<uc1:pageNum ID="pageNum1" runat="server" OnPageDownClick="bindDate" OnPageUpClick="bindDate" OnBtnToPageClick="bindDate" />
</dt>
-----------------------------------页面服务端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBookShop.Account;
namespace WebBookShop
{
public partial class WebForm1 : System.Web.UI.Page
{
int pageRow = 20;//页容量
int pageNow = 1;
string txtSearch = null;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["txtSearch"] != null)
{
txtSearch=Request.QueryString["txtSearch"].ToString();
}
if (!Page.IsPostBack)
{
if (txtSearch != null)
{
pageNow = Convert.ToInt32(this.pageNum1.pageNow);
this.Repeater1.DataSource = getdate(pageNow, pageRow, txtSearch);
this.Repeater1.DataBind();
this.getPageNum(txtSearch);
}
}
//if (Page.PreviousPage!=null|| Page.IsPostBack)//获取向当页传输控件的页(当页的上一页)
//{
// if (PreviousPage.IsPostBack)//判断是否回发提交过来的
// {
// txtSearch=((TextBox) Page.PreviousPage.Controls[0].FindControl("txtSearch")).Text; //获取母版页控件
// int pageNow = Convert.ToInt32(this.pageNum1.pageNow);
// int pageRow = 20;
// this.Repeater1.DataSource = getdate(pageNow,pageRow,txtSearch);
// this.Repeater1.DataBind();
// }
//}
}
*****************************************重点重点重点重点重点********************************************
//触发事件的响应方法 绑定方法<uc1:pageNum ID="pageNum1" runat="server" OnPageDownClick="bindDate"/>
public void bindDate(object obj,EventArgs arg)
{
pageNow = Convert.ToInt32(this.pageNum1.pageNow);
if (txtSearch != null)
{
pageNow = Convert.ToInt32(this.pageNum1.pageNow);
this.Repeater1.DataSource = getdate(pageNow, pageRow, txtSearch);
this.Repeater1.DataBind();
}
}
*********************************重点重点重点重点重点重点重点重*********************************************
/// <summary>
/// 获取指定分页的按关键字搜索的数据
/// </summary>
/// <param name="pageNow"></param>
/// <param name="pageRow"></param>
/// <param name="txtSearch">关键字</param>
/// <returns></returns>
public List<Model.Book> getdate(int pageNow, int pageRow, string txtSearch)
{
int pageMin = (pageNow - 1) * pageRow + 1;
int pageMax = pageRow * pageNow;
string order = "Id";
if (Session["order"] != null)//排序规则
{
order = Session["order"].ToString();
}
List<Model.Book> booksModule = (List<Model.Book>)new BLL.BookBLL().GetPagedData(pageMin, pageMax, txtSearch, order);
return booksModule;
}
/// <summary>
/// 获得页数
/// </summary>
/// <param name="likeStr">搜索关键字</param>
/// <returns></returns>
public void getPageNum(string likeStr)
{
double temnum = new BLL.BookBLL().GetTotalCount(likeStr) / (double)pageRow;
string num= Math.Ceiling((decimal)temnum).ToString();
this.pageNum1.pageRowNum = num;
}
/// <summary>
/// 获得出版社名字
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string getCbsName(int id)
{
return new BLL.PublisherBLL().GetById(id).Name;
}
}
}
--------------------自定义用户控件服务端代码
namespace WebBookShop.UserControls
{
public partial class pageNum : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
conEnalbe();
}
*************************************************************
public event EventHandler PageDownClick = null;//注册事件
public event EventHandler PageUpClick = null;//注册事件
public event EventHandler BtnToPageClick = null;//注册事件
protected void PageDown_Click(object sender, EventArgs e)
{
this.LabelNowPage.Text = (Convert.ToInt32(this.LabelNowPage.Text) + 1).ToString();
this.TexToPage.Text = this.LabelNowPage.Text;
conEnalbe();
if (PageDownClick != null)//当事件不为空
{
PageDownClick(this, null);//触发事件
}
}
********************************************************************
protected void PageUp_Click(object sender, EventArgs e)
{
this.LabelNowPage.Text = (Convert.ToInt32(this.LabelNowPage.Text) - 1).ToString();
this.TexToPage.Text = this.LabelNowPage.Text;
conEnalbe();
if (PageUpClick != null)
{
PageUpClick(this, null);
}
}
protected void BtnToPage_Click(object sender, EventArgs e)
{
this.LabelNowPage.Text = this.TexToPage.Text;
conEnalbe();
if (BtnToPageClick != null)
{
BtnToPageClick(this, null);
}
}
protected void conEnalbe()//改变上一页下一页控件状态
{
if (Convert.ToInt32(LabelNowPage.Text) <= 1)
{
this.PageUp.Enabled = false;
}
else
{
this.PageUp.Enabled = true;
}
if (Convert.ToInt32(LabelNowPage.Text) >= Convert.ToInt32(LabelPageRow.Text))
{
this.PageDown.Enabled = false;
}
else
{
this.PageDown.Enabled = true;
}
}
/// <summary>
/// 当前页
/// </summary>
public string pageNow
{
get { return this.LabelNowPage.Text; }
set { this.LabelNowPage.Text = value; }
}
/// <summary>
/// 页数
/// </summary>
public string pageRowNum
{
get { return this.LabelPageRow.Text; }
set { this.LabelPageRow.Text = value; }
}
/// <summary>
/// 跳转到*页
/// </summary>
public string GoPage
{
get { return this.TexToPage.Text; }
set { this.TexToPage.Text = value; }
}
}
}