GridView PageIndexChanging事件未在UpdatePanel内部触发

时间:2017-10-07 15:47:41

标签: javascript c# asp.net gridview

我没有找到解决问题的方法:

  

GridView分页事件gvBanquet_PageIndexChanging在UpdatePanel内部时不会触发。

问题代码在这里:

<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>

这是一个分页事件:

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

   // fill and bind gridview here
}

我添加了一个断点,但它没有触发。

enter image description here

如何触发分页按钮?

enter image description here

2 个答案:

答案 0 :(得分:3)

GridView没有绑定方法所以我提供了两种方法(两种方法都有效) 方法1 :与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>

不需要任何代码。

方法2 :从代码背后绑定:

.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

代码为VB.NET。我希望OP可以将其翻译为C#

答案 1 :(得分:0)

没有看到getdata代码的可能情况之一是:

每次更改页面索引时,都应该读取用于加载gridview的数据集(您的案例Datatable)。通过这种方式,您可以确保在gridview页码触发的每个单独的回发中,将填充结果。

<强>更新

尝试添加触发器

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