user1773603
user1773603

Reputation:

GridView PageIndexChanging event is not firing inside UpdatePanel

I have not found any solution of my problem:

GridView paging event gvBanquet_PageIndexChanging not firing when it is inside an UpdatePanel.

As problem code is here:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
  <ContentTemplate>

  <div class="col-lg-12 table-responsive">
  <asp:GridView ID="gvBanquet" runat="server" AutoGenerateColumns="false" 
      OnRowCommand="gvBanquet_RowCommand" AllowPaging="True" PageSize="5" 
      EmptyDataText="No record found!" OnPageIndexChanging="gvBanquet_PageIndexChanging" 
      ShowHeaderWhenEmpty="true">
      <Columns>

      // here are templates 

      </Columns>
      <PagerStyle CssClass="pagination-ys" />
  </asp:GridView>

  </div>
  <!-- /.col-lg-12 -->

  </ContentTemplate>
  <Triggers>
      <asp:AsyncPostBackTrigger ControlID="gvBanquet" EventName="PageIndexChanging" />
  </Triggers>
</asp:UpdatePanel>

And here is a paging event:

protected void gvBanquet_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvBanquet.PageIndex = e.NewPageIndex;

   // fill and bind gridview here
}

I have added a break point but its not firing.

enter image description here

How I can fire paging buttons?

enter image description here

Upvotes: 0

Views: 2141

Answers (2)

Tummala Krishna Kishore
Tummala Krishna Kishore

Reputation: 8271

One of the possible case without seeing your getdata code is that :

you should read the data set (your case Datatable ) which was used to load the gridview, each time your page index is changed. By this way, you could ensure that in each seperate postback which has been triggered by the gridview page number, results will be populated.

Update

Try to add a trigger

<Triggers>
        <asp:AsyncPostBackTrigger ControlID="gvBanquet" EventName="PageIndexChanging" />
 </Triggers>

Upvotes: 0

Alex Kudryashev
Alex Kudryashev

Reputation: 9460

There is no binding method of the GridView so I offer two methods (both works)
Method 1: Declarative binding with SqlDataSource:

<form id="form1" runat="server">
    <%--ScriptManager required by UpdatePanel --%>
    <asp:ScriptManager runat="server" ID="sm" />
<div>
    <asp:UpdatePanel runat="server" UpdateMode="Conditional">
        <ContentTemplate>
    <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" AllowPaging="true" PageSize="5" DataKeyNames="ProductID" DataSourceID="sqlNWind">
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
            <asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <%--Triggers are redundant because GridView is inside the UpdatePanel --%>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:SqlDataSource ID="sqlNWind" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindCnn %>"
        SelectCommand="SELECT Products.ProductID, Products.ProductName, Products.CategoryID, Categories.CategoryName FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID">
    </asp:SqlDataSource>
</div>
</form>

No Code Behind required.

Method 2: Binding from Code Behind:

.aspx
<form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="sm" />
<div>
    <asp:UpdatePanel runat="server" UpdateMode="Conditional">
        <ContentTemplate>
    <asp:GridView ID="gvProducts" runat="server" OnPageIndexChanging="gvProducts_PageIndexChanging" AutoGenerateColumns="False" AllowPaging="true" PageSize="5" DataKeyNames="ProductID">
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
            <asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>

.aspx.vb

Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        BindProds()''//Bind initial
    End If
End Sub
Protected Sub gvProducts_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    gvProducts.PageIndex = e.NewPageIndex
    BindProds()''//Bind when pageIndex changing
End Sub

Private Sub BindProds()
    Using cnn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("NorthwindCnn").ConnectionString)
        Using cmd As New Data.SqlClient.SqlCommand("SELECT Products.ProductID, Products.ProductName, Products.CategoryID, Categories.CategoryName FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID", cnn)
            Dim da As New Data.SqlClient.SqlDataAdapter(cmd)
            Dim dt As New Data.DataTable()
            da.Fill(dt)
            da.Dispose()
            gvProducts.DataSource = dt
            gvProducts.DataBind()
        End Using
    End Using
End Sub

The code is VB.NET. I hope OP can translate it to C#.

Upvotes: 4

Related Questions