日期:2014-05-20  浏览次数:20448 次

AspNetPager+updatepanel+repeater无刷新分页问题,急需解答
HTML code

    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" 
        style="width:566px; float:left;" ClientIDMode="AutoID" 
        ViewStateMode="Enabled">
        <ContentTemplate>
            
               <asp:Repeater ID="adlistRP" runat="server" ViewStateMode="Enabled" OnItemCommand="adlistRP_ItemCommand" >
               <ItemTemplate>
                 <div class="content">
                   <asp:Image ID="Image1"  ImageUrl='<%#Eval("ss_pic") %>' runat="server" style="margin:5px;width:60px;height:60px;float:left;" />
                   <div style="width:490px;float:right">
                   <div style="margin:5px 0;width:490px;float:right;">
                     <a><%#Eval("ss_name") %>:</a><%#Eval("ad_word") %><%#Eval("ad_id") %>
                   </div>
                   <div style="float:right;width:490px;height:25px;">
                       <div style="width:40px;height:25px;float:right; padding-left:3px; border-left:1px solid #dddddd; ">
                       <asp:Button ID="Button2" CommandName="1" CommandArgument='<%#Eval("ad_id")%>' runat="server" Text="评论" style="background-color:white; border: 0px; height:28px; cursor:pointer;" />
                       </div>
                   </div>
                   </div>
                 </div>
               </ItemTemplate>
            </asp:Repeater>
           <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="610px" FirstPageText="第一页" 
         LastPageText="最后一页" NextPageText="下一页" PrevPageText="上一页" Font-Names="Arial" 
            BackColor="#F8B500" AlwaysShow="true"  
         ShowInputBox="Always" SubmitButtonText="转到" SubmitButtonStyle="botton" 
            OnPageChanging="pageBind_PageChanging" ForeColor="Black" 
         HorizontalAlign="Center" PageSize="10" 
         style="font-size: 12px; color: #0099ff; background-color: #ffffff; text-decoration: none" 
         UrlPaging="false" InvalidPageIndexErrorMessage="请请输入正确的页码!" 
            ShowNavigationToolTip="True" TextAfterInputBox="页" TextBeforeInputBox="第" 
            PageIndexBoxType="DropDownList" PageIndexOutOfRangeErrorMessage="请请输入正确的页码!" 
            ShowPageIndexBox="Always" ViewStateMode="Enabled" >
    </webdiyer:AspNetPager>
        </ContentTemplate>

    </asp:UpdatePanel>



C# code

    protected string rank { get; set; }//用于存放排名广告读取的sql语句
    protected string free { get; set; }//用于存放免费广告读取的sql语句
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            rank = "select * from adlist where ad_top='N'";//我随便写的试验用的sql语句。
            free = "select * from adlist where ad_top='Y'";//我随便写的试验用的sql语句。
            BindData(rank, free);

        }

        

    }



    //数据绑定
    protected void BindData(string rank,string free)
    {
        DBaction db = new DBaction();
        DataTable dt1 = db.Select(rank);//获取排名广告
        DataTable dt2 = db.Select(free);//获取免费广告
        DataTable dt = new DataTable();
        dt = dt1;
        dt.Merge(dt2);//将排名广告与免费广告何为一张表
        AspNetPager1.PageSize = 10;
        AspNetPager1.RecordCount = dt.Rows.Count;
        
        //设置一个新表list,内容为要绑定的信息
        DataTable list = dt.Clone();//复制dt的表结构
        
        //设置最后一条记录在list中的index值。
        int LastSize=AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1)+AspNetPager1.PageSize-1;
        
        //如果为最后一页则,lastsize取值为list表中的最大index值
        if (LastSize > dt.Rows.Count - 1) { LastSize = dt.Rows.Count - 1; }
        
        //将要绑定的信息从dt表中放入list表
        for (int i = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1); i <= LastSize; i++) 
        {
            list.Rows.Add(dt.Rows[i].ItemArray);
        }
      
        //创建dataset ds,将list表放入
        DataSet ds = new DataSet();
        ds.Tables.Add(list);
        
        //绑定repeater
        adlistRP.DataSource = ds.Tables[0];
        adlistRP.DataBind();

    }

    //翻页事件
    protected void pageBind_PageChanging(object src, PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        BindData(rank,free);
    }