C# – GridView PAGING inside UpdatePanel does not work for second page change, why

asp.netcgridviewpagingupdatepanel

I got two updatepanels which one of them contains a gridview. The problem is the Pagination works properly only for the first time.

Here is the markup:

<asp:UpdatePanel ID="upAnswers" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>
        <asp:GridView ID="gvAnswers" runat="server" CssClass="gv" Width="100%" ShowHeader="false" AllowPaging="True" PageSize="2" EnableSortingAndPagingCallbacks="true" OnPageIndexChanging="gvAnswers_PageIndexChanging" AutoGenerateColumns="False" AlternatingRowStyle-BackColor="#eee">
            <Columns>
                <asp:TemplateField HeaderText="Questions" ShowHeader="False">
                    <ItemTemplate>
                        <asp:Label ID="lblQuestions" onclick=<%# Eval("FaqID", "$('.lblAnswers_{0}').toggle('fast')") %> CssClass="block" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                        <asp:Panel ID="pnlS1" runat="server" CssClass='<%# Eval("FaqID", "lblAnswers_{0}") %>' style="display: none;">
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Question") %>' CssClass="block bold"></asp:Label>
                            <asp:Label ID="lblAnswers" runat="server" Text='<%# Eval("Answer") %>'></asp:Label>
                        </asp:Panel>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="upgAnswers" runat="server" AssociatedUpdatePanelID="upAnswers" DisplayAfter="300">
    <ProgressTemplate>
        <div id="uiBlock" class="uiBlock">
        </div>
    </ProgressTemplate>
</asp:UpdateProgress>

and the code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetAnsweredPublicFAQs();
    }
}

protected void GetAnsweredPublicFAQs()
{
    DataSet ds = cFAQs.getAnsweredPublicFAQs();

    gvAnswers.DataSource = ds;
    gvAnswers.DataBind();
}

protected void gvAnswers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvAnswers.PageIndex = e.NewPageIndex;
    GetAnsweredPublicFAQs();
    gvAnswers.DataBind();
    upAnswers.Update();
}

As ref, I use ASP.NET 4.0 C#.

Any kind help would be highly appreciated.

Regards,
Kardo

Best Answer

Change your UpdatePanel declaration to start like this:

<asp:UpdatePanel ID="upAnswers" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="gvAnswers" EventName="PageIndexChanging" />

Whenever I've had trouble with these UpdatePanels, I find explicitly declaring the Triggers to be helpful. Especially when you're using a "non-default" event to trigger an AsyncPostBack (SelectedIndexChanged is the "default" event for the GridView).

Also, ChildrenAsTriggers defaults to true, so I removed that from the markup.

Related Topic