代码改变世界

DataPager控件通过URL传参保存当前页码

2014-03-24 10:03  mikx  阅读(358)  评论(0编辑  收藏  举报

从ListView当前页Default.aspx点击修改超链接,跳转到修改页面Mod.aspx

<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1" onsorting="ListView1_Sorting" ondatabound="ListView1_DataBound" >
    <LayoutTemplate> 
    <table> 
        <thead> 
            <tr> 
                <th>修改</th> 
            </tr>
        </thead>
        <tbody> 
            <asp:PlaceHolder runat="server" ID="itemPlaceholder"> </asp:PlaceHolder> 
        </tbody>
   </table> 
   </LayoutTemplate> 
   <ItemTemplate> 
   <tr> 
        <td>
            <a href='Mod.aspx?customerid=<%# Eval("CustomerID") %>&p=<%=Data_Pager_Index %>'>修改</a>
        </td> 
   </tr> 
   </ItemTemplate> 
</asp:ListView>                    

 

 上面html代码中,参数customerid是当前行ID,参数p是当前页码,Data_Pager_Index是后台代码 Default.aspx.cs中的字段,在每次通过点击DataPager分页控件使ListView控件绑定数据后,由事件 ListView1_DataBound中修改并保存当前页码:

protected void ListView1_DataBound(object sender, EventArgs e)
        {
            Data_Pager_Index = DataPager1.TotalRowCount > 0 ? (DataPager1.StartRowIndex / DataPager1.PageSize + 1) : 0;
        }

在修改页面操作完成后,在提交或返回按钮中将获取的Data_Pager_Index页码传递回Default.aspx:

protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?page=" + P + "");
        }

在Default.aspx页面中获取page参数的值,通过自定义方法SetPagerIndex()设置DataPager参数:

protected void SetPagerIndex()
        {
            int startindex = (Data_Pager_Index - 1) * DataPager1.PageSize;
            int pagesize = DataPager1.PageSize;
            DataPager1.SetPageProperties(startindex, pagesize, true);
            ListView1.DataBind();
        }

该方法中通过DataPager控件的SetPageProperties()方法设置当前页起始索引startindex及当前页显示记录pagesize,是否绑定数据中设置为true,然后绑定数据,就可返回跳转到修改页Mod.aspx之前的页面。