Wayne
Wayne

Reputation: 6449

ASP.NET - ListView and DataPager not working

I used a DataPager to page through ListView data. When ListView bound, DataPager show many pages below, everything seems to be ok but when i click to another page, After a postback, ListView is empty with EmptyDataTemplate and of course without pages.

I googled and try one and both workarounds below but they didn't help

    protected void DatapPager_OnPreRender(object sender, EventArgs e)
    {
        try
        {
            listView.DataSource = ((DataSet)ViewState[VIEWSTATE_DATASET]).Tables[0];
            listView.DataBind();
        }
        catch (System.Exception ex)
        {               
        }            
        base.OnPreRender(e);
    }

    protected void listView_OnPagePropertiesChanged(object sender, EventArgs e)
    {
        try
        {
            listView.DataSource = ((DataSet)ViewState[VIEWSTATE_DATASET]).Tables[0];
            listView.DataBind();
        }
        catch (System.Exception ex)
        {               
        }            
    }

In my case, ListView's data bound in a click event, not on PageLoad.

Anyone can helps me? Thank you so much!

Upvotes: 1

Views: 1369

Answers (2)

Mustafa Iqbal
Mustafa Iqbal

Reputation: 73

Its Simple, Just Get "ID" in "QUERY-STRING" from the Database, Now Set it to the Pager Control Property as [QueryStringField="ID"] like:

<asp:DataPager ID="DataPagerProducts" runat="server" QueryStringField="ID" PageSize="3">
                            <Fields>
                                <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
                                <asp:NumericPagerField />
                                <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
                            </Fields>
                        </asp:DataPager>

Note: if not woking, then set also [PagedControlID="ListView_Name"].

Upvotes: 1

rick schott
rick schott

Reputation: 21112

You don't show how ViewState[ViewState[VIEWSTATE_DATASET] gets set. I am guessing this is getting reset to null on postback and you are swallowing the Exception that is probably raised on this line trying to cast a null value:

 listView.DataSource = ((DataSet)ViewState[VIEWSTATE_DATASET]).Tables[0];

Upvotes: 0

Related Questions