ProtoTyPus
ProtoTyPus

Reputation: 1324

C# ASP.NET, DataPager doesn't change page when i click it the first time

I have a problem.

I've written this code:

<asp:ListView ID="ListComment" runat="server">
        <LayoutTemplate>
            <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
        </LayoutTemplate>
        <ItemTemplate>
            <div class="CommentArea" id='Comment<%#Eval("Id")%>'>
                <div class="UserDate">
                    <span class="font07em fontunderline"><b>Inviato da: </b></span><span class="font07em"><b><a href="#"><%#Eval("Autore") %></a></b></span><span class="font07em fontunderline"><b> alle <%#Eval("Orario", "{0:hh':'mm}") %></b></span>
                </div>
                <div class="LikeDontLike">
                    <span class="font07em"><b>
                        <asp:LinkButton runat="server" ID="Like" CommandArgument='<%#Eval("Id")%>' OnClick="Mipiace_Click" >Like</asp:LinkButton>: <%#Eval("Mipiace") %> /
                        <asp:LinkButton runat="server" ID="DontLike" CommandArgument='<%#Eval("Id")%>' OnClick="Nonmipiace_Click" >Don't Like</asp:LinkButton>: <%#Eval("Nonmipiace") %></b></span>
                </div>
                <div class="UserComment">
                    <span class="font07em"><%#Eval("Commento") %></span>
                </div>
            </div>
        </ItemTemplate>
    </asp:ListView>
    <asp:DataPager runat="server" ID="PageComment" PagedControlID="ListComment" PageSize="2">
        <Fields>
            <asp:NumericPagerField />
        </Fields>
    </asp:DataPager>

When I made run it and I click on a page of DataPager it doesn't change the view. If I click a second time, it changes. I'have tried to put the DataPager into the LayoutTemplate, but I have always the same problem. How I can solve it? Thanks all!

Upvotes: 0

Views: 2189

Answers (3)

Anthony J
Anthony J

Reputation: 73

I had this same issue too and learned that my rendering the data on my Page_Load event was messing with the rendered data on the List view. My solution was to add an OnPreRender event for the data pager and render my data there instead of on the Page_Load.

This is a great example: ASP.Net : DataPager Control always a step behind with paging

Upvotes: 0

M0-3E
M0-3E

Reputation: 1042

I suppose you're using the page_load event to bind your listview to your datasource. You need to add a new handle the PagePropertiesChanged event of the listview to rebind your listview again. At the end, your databinding code would be duplicated in the page_load event (not need to check for postback) and in the listview_PagePropertiesChanged event.

Upvotes: 2

Tan
Tan

Reputation: 1

You need to add

    <asp:ListView ID="ListComment" runat="server" OnPagePropertiesChanging="Listcomment_PagePropertiesChanging">

Then in the codebehind

protected void Listcomment_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    this.ListComment.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

    // Rebind your ListComment
}

Upvotes: -1

Related Questions