ASP.NET DataPager

时间:2011-03-11 13:39:48

标签: asp.net listview datapager

我正在尝试将ASP.NET DataPager控件链接到ASP.NET ListView

<div>
    <asp:ListView ID="ListView1" runat="server">
        <ItemTemplate>
            <a href="#"><%#DataBinder.Eval(Container.DataItem,"LinkText")%></a>
        </ItemTemplate>           
    </asp:ListView>

    <asp:DataPager runat="server" ID="DataPager1" PagedControlID="ListView1" PageSize="2">
        <Fields>
            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
        </Fields>
    </asp:DataPager>        

</div>

在Page_Load中,我使用DataTable加载ListView

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            _fl.ConnectToSQLServer();

            DataTable dt = new DataTable();

            dt.Load(_fl.GetCategories());

            ListView1.DataSource = dt;
            ListView1.DataBind();

            _fl.DisconnectFromSQLServer();
        }
    }

数据显示正确,但DataPager不能正常工作

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:4)

从Page_Load切换到OnPreRender以填充ListView并跳过IsPostBack检查

尝试

protected override void OnPreRender(EventArgs e)
{
            base.OnPreRender(e);
           _fl.ConnectToSQLServer();

            DataTable dt = new DataTable();

            dt.Load(_fl.GetCategories());

            ListView1.DataSource = dt;
            ListView1.DataBind();

            _fl.DisconnectFromSQLServer();   


}