我用PagedDataSource对象分页,怎么PageSize设置了没用??
发布网友
发布时间:2024-10-02 09:10
我来回答
共3个回答
热心网友
时间:2024-10-06 16:44
前台;<DIV style="WIDTH: 608px; HEIGHT: 21px" id="div2" runat="server">
<asp:Label id="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton id="lbtnTop" onclick="lbtnTop_Click" runat="server">首页</asp:LinkButton>
<asp:LinkButton id="lbtnPrev" onclick="lbtnPrev_Click" runat="server">上一页</asp:LinkButton>
<asp:LinkButton id="lbtnNext" onclick="lbtnNext_Click" runat="server">下一页</asp:LinkButton>
<asp:LinkButton id="lbtnBottom" onclick="lbtnBottom_Click" runat="server">尾页</asp:LinkButton>
转到第<asp:TextBox onkeydown="return CheckNumber()" id="textCount" runat="server" Width="35px" Height="15px" BorderColor="Silver" BorderWidth="1px"></asp:TextBox> 页
<asp:Button id="btnGo" onclick="btnGo_Click" runat="server" Height="22px" Text="Go"></asp:Button>
</DIV>
后台:
PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["page"] = 0;//用于保存当前页数
DisplayGoods();
}
}
/// <summary>
/// 绑定商品列表信息
/// </summary>
public void DisplayGoods()
{
List<Goods> gList = GoodsManager.GetAllGoods();
if (gList.Count == 0)
{
div2.Visible = false;
msg.Visible = true;
}
else
{
pds.DataSource = gList;//把数据源赋值给pagedatasource
pds.AllowPaging = true;//启用分页功能
pds.PageSize = 10;//每页显示10条数据
pds.CurrentPageIndex = Pager;//保存当前页数
lblCurrentPage.Text = "当前第" + (pds.CurrentPageIndex + 1).ToString() + "页/共" + pds.PageCount.ToString() + "页";
ViewState["total"] = pds.PageCount;//保存总页数
SetEnable(pds);//按钮按钮的状态
gvGoods.DataSource = pds;//把分页后的数据源赋值给datalist;
gvGoods.DataBind();
}
}
/// <summary>
/// 获取图片的路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string GetPath(string path)
{
path = Server.MapPath("~/Flower/"+path);
return path;
}
//将当前页数作为本页的属性
public int Pager
{
get
{
return (int)ViewState["page"];
}
set
{
ViewState["page"] = value;
}
}
/// <summary>
/// 设置上下页按钮的有效状态
/// </summary>
public void SetEnable(PagedDataSource pads)
{
lbtnPrev.Enabled = true;
lbtnNext.Enabled = true;
lbtnTop.Enabled = true;
lbtnBottom.Enabled = true;
if (pds.IsFirstPage)//如果当前是第一页,就把上一页和首页的按钮禁用
{
lbtnPrev.Enabled = false;
lbtnTop.Enabled = false;
}
if (pds.IsLastPage)//如果当前是最后一页,就把下一页和尾页的按钮禁用
{
lbtnNext.Enabled = false;
lbtnBottom.Enabled = false;
}
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnTop_Click(object sender, EventArgs e)
{
Pager = 0;
DisplayGoods();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnPrev_Click(object sender, EventArgs e)
{
Pager--;
DisplayGoods();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnNext_Click(object sender, EventArgs e)
{
Pager++;
DisplayGoods();
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnBottom_Click(object sender, EventArgs e)
{
Pager = Convert.ToInt32(ViewState["total"]) - 1;//取出总页数
DisplayGoods();
}
/// <summary>
/// 转到
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnGo_Click(object sender, EventArgs e)
{
int pag = Convert.ToInt32(textCount.Text);//获取用户输入的页数
if (pag >= 0 && pag <= Convert.ToInt32(ViewState["total"]))//如果用户输入的页数在合理范围内,则跳转
{
Pager = pag - 1;
DisplayGoods();
}
}
热心网友
时间:2024-10-06 16:48
DataList1.DataSource = ds;
改成DataList1.DataSource = ps;
试试
热心网友
时间:2024-10-06 16:46
你在页面 设置允许 分页了吗?