DataPager分页问题,ListView重新绑定数据源后DataPager未回到初始页?

如题:
ListView用ObjectDataSource做数据源

当根据查询条件重新绑定后,DataPager没有回到初始页

就是原来我如果选择了第三页,ListView正常显示出来

 这时候根据查询条件ListView数据源变了,但是DataPager还是停留在了第三页,怎么让它回到初始页?

通过自己的各种实验,得到方法如下:

前台代码是这样的:(但是就是因为后台代码的问题,会造成标题中的问题)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepositoryList.aspx.cs"
    Inherits="ZWW.Web._2010.Knowledge.RepositoryList" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="asp" %>
<!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">
    <div>
        <asp:Repeater runat="server" ID="repList_root">
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <asp:LinkButton ID="lb_root" runat="server" CommandName="Root" OnCommand="LinkButton_Command"
                        Text='<%#Eval("CATEGORY_NAME")%>' CommandArgument='<%#Eval("CATEGORY_ID")%>'></asp:LinkButton></li>
            </ItemTemplate>
            <FooterTemplate>
                </ul></FooterTemplate>
        </asp:Repeater>
    </div>
    <div>
        <asp:ListView runat="server" ID="repList" DataSourceID="ods_repository">
            <LayoutTemplate>
                <div>
                    <ul>
                        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                    </ul>
                </div>
            </LayoutTemplate>
            <ItemTemplate>
                <li>
                    <%#Eval("Q_TITLE")%></li>
                <li>
                    <%#Eval("A_CONTENT")%></li>
            </ItemTemplate>
        </asp:ListView>
        <asp:HiddenField ID="hf_category" runat="server" />
        <div class="alPage">
            <asp:DataPager ID="dp_party" runat="server" PagedControlID="repList" PageSize="2">
                <Fields>
                    <asp:TemplatePagerField>
                        <PagerTemplate><%= DataCount%>条记录, 当前第<asp:Label runat="server" ID="CurrentPageLabel" Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />/<asp:Label
                                runat="server" ID="TotalPagesLabel" Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />页,
                        </PagerTemplate>
                    </asp:TemplatePagerField>
                    <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="true" ShowPreviousPageButton="true"
                        ShowLastPageButton="false" ShowNextPageButton="false" FirstPageText="首页" ButtonCssClass="PageCss"
                        PreviousPageText="上页" />
                    <asp:NumericPagerField />
                    <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="true"
                        ShowFirstPageButton="false" ShowPreviousPageButton="false" LastPageText="末页"
                        ButtonCssClass="PageCss"
                        NextPageText="下页" />
                </Fields>
            </asp:DataPager>
             <asp:ObjectDataSource ID="ods_repository" runat="server" SelectMethod="GetData"
                            TypeName="ZWW.BLLNEW.ZZK.PageBusiness" MaximumRowsParameterName="pageSize" StartRowIndexParameterName="startIndex"
                            EnablePaging="True" SelectCountMethod="GetAmount" EnableViewState="false">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="hf_category" Name="category_id" PropertyName="Value"
                                    Type="String" />
                            </SelectParameters>
                        </asp:ObjectDataSource>
        </div>
    </div>
    </form>
</body>
</html>

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZWW.BLL.Sites.Model;
using ZWW.BLL;
using ZWW.BLL.IndexWeb;
using ZWW.BLLNEW.ZZK;

namespace ZWW.Web._2010.Knowledge
{
    public partial class RepositoryList : System.Web.UI.Page
    {
        public int DataCount
        {
            get
            {
                return dp_party.TotalRowCount;
            }
        }
        PageBusiness PB = new PageBusiness();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageInit();
            }
        }
        void PageInit()
        {            
            repList_root.DataSource = PB.GetRoot();
            repList_root.DataBind();
            if (repList_root.Items.Count > 0)
            {
                LinkButton temp = repList_root.Items[0].FindControl("lb_root") as LinkButton;
                hf_category.Value = temp.CommandArgument.ToString();
            }
        }

        protected void LinkButton_Command(Object sender, CommandEventArgs e)
        {
            hf_category.Value = e.CommandArgument.ToString();
            dp_party.SetPageProperties(0, 2, false);
        }

    }
}

关键性的代码就是: dp_party.SetPageProperties(0, 2, false);

在这里重新对DataPager的属性进行设置(设置为回到初始页,pagesize为2),然后ObjectDataSource再通过反射进行数据的绑定

这里有个地方注意的是:ObjectDataSource的数据绑定是在按钮回发事件之后

原文地址:https://www.cnblogs.com/wenghaowen/p/3340308.html