gpt4 book ai didi

javascript - GridView PageIndexChanging 事件未在 UpdatePanel 内触发

转载 作者:行者123 更新时间:2023-11-28 17:46:28 30 4
gpt4 key购买 nike

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

GridView paging event gvBanquet_PageIndexChanging not firing when it is inside an 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

最佳答案

GridView没有绑定(bind)方法,所以我提供两种方法(都有效)
方法 1:与 SqlDataSource 的声明式绑定(bind):

<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:从代码隐藏进行绑定(bind):

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

关于javascript - GridView PageIndexChanging 事件未在 UpdatePanel 内触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46622120/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com