分页在 AJAX updatepanel 内的 asp.net gridview 中不起作用

发布于 2024-08-03 15:12:39 字数 4002 浏览 1 评论 0原文

我有一个最初绑定到 sqldatasource 控件的 asp.net gridview,但是当用户按下外部按钮时,它会获取数据表而不是 SQLdatasource 控件的内容。因此,我必须在 gridview 的 PageIndexChanging 事件中编写代码以允许分页。我的代码如下:

Protected Sub gvEvents_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvEvents.PageIndexChanging

gvEvents.PageIndex = e.NewPageIndex

gvEvents.DataBind()

这工作得很好,直到我添加了一个 AJAX 更新面板,这样整个页面就不会在每次分页时回发,并且分页停止工作。我调试了一下,发现它实际上是在调用PageIndexChanging事件,但什么也没发生。

我在网上搜索了一下,发现有几个人有同样的问题,但他们的解决方案对我不起作用。该网站上有人通过以下方法解决了该问题:

在 PageIndexchanging 事件中,将数据绑定到网格,确保再次从数据库获取数据

我不知道这意味着什么;我的数据如上所示被绑定。我将“启用分页”设置为 true,将 EnableSortingAndPagingCallbacks 设置为 false。

如果有人能帮助我,我将非常感激。我在下面的更新面板中添加了我的标记。太感谢了!

    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ibtnSearch" />           
        </Triggers>

        <ContentTemplate>

            <asp:GridView ID="gvEvents" runat="server"  DataKeyNames = "intID"
                AutoGenerateColumns="False" AllowPaging="True" GridLines="None" CellPadding="10" 
                ForeColor="#333333" PageSize="6" DataSourceID="defaultDS" >
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

                <Columns>
                    <asp:TemplateField HeaderText="Date">
                        <ItemTemplate>
                        <!-- put code block inside label? To set the formatter to include year if 
                        it's next year? -->
                        <asp:Label ID="Label1" runat="server" 
                            Text = '<%# RepeatingMethods.DetermineOngoing(CType(Eval("dtmEventStartDate"), DateTime) , CType(Eval("dtmEventEndDate"), DateTime))%>'> </asp:Label>

                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                  <ItemTemplate>
                  <asp:Label ID="Label4" runat="server" Text='<%# Eval("chvAgeRange")  %>'> </asp:Label> <br />
                   <asp:Label ID="Label3" runat="server" Text= '<%# Eval("chvState")  %>'> </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:HyperLinkField DataNavigateUrlFields="intId" 
                    DataNavigateUrlFormatString="EventDetail.aspx?intId={0}" 
                    DataTextField="chvEventName" />

                    <asp:BoundField DataField="chvBriefDescription" HeaderText="Description" 
                        SortExpression="chvBriefDescription" />


            </Columns>
                     <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                     <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                     <AlternatingRowStyle BackColor="White" />


<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></FooterStyle>

<PagerStyle HorizontalAlign="Center" BackColor="#FFCC66" ForeColor="#333333"></PagerStyle>

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy"></SelectedRowStyle>

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></HeaderStyle>

<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
    </asp:GridView>



                <asp:Label ID="lblError" runat="server"></asp:Label>

    <br />
    </ContentTemplate>
    </asp:UpdatePanel>

I have an asp.net gridview that is originally bound to a sqldatasource control, but when the user presses an external button, it instead gets the contents of a datatable rather than a SQLdatasource control. I therefore had to write code in the PageIndexChanging event of the gridview to allow for paging. My code is as follows:

Protected Sub gvEvents_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvEvents.PageIndexChanging

gvEvents.PageIndex = e.NewPageIndex

gvEvents.DataBind()

This worked beautifully until I added an AJAX update panel so the whole page wouldn't postback every time it paged, and paging stopped working. I debugged it and discovered that it is actually calling the PageIndexChanging event, but nothing is happening.

I searched the web and found a few people with the same problem, but their solutions did not work for me. There was one on this site whose problem was solved by the following:

In PageIndexchanging event, where you bind data to grid, make sure, data is again fetched from the DB

I don't know what that means; my data was being bound as demonstrated above. I have "enable paging" set to true and EnableSortingAndPagingCallbacks set to false.

I would really appreciate if someone can help me. I am including my markup for the updatepanel below. Thank you so much!

    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ibtnSearch" />           
        </Triggers>

        <ContentTemplate>

            <asp:GridView ID="gvEvents" runat="server"  DataKeyNames = "intID"
                AutoGenerateColumns="False" AllowPaging="True" GridLines="None" CellPadding="10" 
                ForeColor="#333333" PageSize="6" DataSourceID="defaultDS" >
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

                <Columns>
                    <asp:TemplateField HeaderText="Date">
                        <ItemTemplate>
                        <!-- put code block inside label? To set the formatter to include year if 
                        it's next year? -->
                        <asp:Label ID="Label1" runat="server" 
                            Text = '<%# RepeatingMethods.DetermineOngoing(CType(Eval("dtmEventStartDate"), DateTime) , CType(Eval("dtmEventEndDate"), DateTime))%>'> </asp:Label>

                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                  <ItemTemplate>
                  <asp:Label ID="Label4" runat="server" Text='<%# Eval("chvAgeRange")  %>'> </asp:Label> <br />
                   <asp:Label ID="Label3" runat="server" Text= '<%# Eval("chvState")  %>'> </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:HyperLinkField DataNavigateUrlFields="intId" 
                    DataNavigateUrlFormatString="EventDetail.aspx?intId={0}" 
                    DataTextField="chvEventName" />

                    <asp:BoundField DataField="chvBriefDescription" HeaderText="Description" 
                        SortExpression="chvBriefDescription" />


            </Columns>
                     <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                     <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                     <AlternatingRowStyle BackColor="White" />


<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></FooterStyle>

<PagerStyle HorizontalAlign="Center" BackColor="#FFCC66" ForeColor="#333333"></PagerStyle>

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy"></SelectedRowStyle>

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></HeaderStyle>

<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
    </asp:GridView>



                <asp:Label ID="lblError" runat="server"></asp:Label>

    <br />
    </ContentTemplate>
    </asp:UpdatePanel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一杯敬自由 2024-08-10 15:12:39

在 PageIndexchanging 事件中,将数据绑定到网格,确保再次从数据库中获取数据
我不知道这意味着什么;我的数据如上所示被绑定。

这意味着您需要在代码隐藏页面中再次获取数据。您在 design/html 页面中使用 SQLdatasource,因此您需要删除它并使用 SQL 连接、SQL 命令等来获取数据,然后将其设置为控件的数据源。

如下所示:

http://www.aspnettutorials.com/ Tutorials/database/db-grid-aspnet2-vb.aspx

你的代码应该是这样的

Protected Sub Page_Load(...)
   gvEvents.PageIndex = 0
   LoadData();// loads initial data
end sub

private sub LoadData()
 '' do your SQL Conn and Command here
 '' set your datasource of gridview here
end sub

Protected Sub gvEvents_PageIndexChanging(...) Handles gvEvents.PageIndexChanging
  gvEvents.PageIndex = e.NewPageIndex
  LoadData()
  gvEvents.DataBind()
end sub

In PageIndexchanging event, where you bind data to grid, make sure, data is again fetched from the DB
I don't know what that means; my data was being bound as demonstrated above.

It means that you need to fetch your data again in your code behind page. You are using a SQLdatasource in your design/html page, so you need to remove that and use a SQL Connection, SQL Command, etc. to fetch your data and then set that as your control's datasource.

Something like below:

http://www.aspnettutorials.com/tutorials/database/db-grid-aspnet2-vb.aspx

Your code should look something like this

Protected Sub Page_Load(...)
   gvEvents.PageIndex = 0
   LoadData();// loads initial data
end sub

private sub LoadData()
 '' do your SQL Conn and Command here
 '' set your datasource of gridview here
end sub

Protected Sub gvEvents_PageIndexChanging(...) Handles gvEvents.PageIndexChanging
  gvEvents.PageIndex = e.NewPageIndex
  LoadData()
  gvEvents.DataBind()
end sub
初熏 2024-08-10 15:12:39

对于任何偶然发现这个问题的人来说,我今天上午遇到了这个问题,并且这篇文章具有误导性(至少对于我的情况而言)。对我来说,我只需添加一个 PageIndexChanging 事件作为 updatepanel 触发器的数据网格的触发器,它就解决了我的问题。

For anyone who stumbles across this issue, I encountered it this AM and this post was misleading (atleast for my scenario). For me, I simply had to add a PageIndexChanging Event as a trigger for my datagrid for the updatepanel trigger's, and it resolved my issue.

残疾 2024-08-10 15:12:39

与 UpdatePanel 控件不兼容的控件

以下 ASP.NET 控件与部分页面更新不兼容,因此在 UpdatePanel 控件内不受支持:

GridViewDetailsView控制它们的 EnableSortingAndPagingCallbacks 属性何时设置为 true。默认为 false。

http://www.asp.net/Ajax/Documentation/Live/概述/UpdatePanelOverview.aspx

Controls that Are Not Compatible with UpdatePanel Controls

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside an UpdatePanel control:

GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.

http://www.asp.net/Ajax/Documentation/Live/overview/UpdatePanelOverview.aspx

吖咩 2024-08-10 15:12:39

只需在 databind() 之后更新 AJAX 面板即可。

假设更新面板的 id 是 AJAXPanel

Protected Sub gvEvents_PageIndexChanging(...) Handles gvEvents.PageIndexChanging             
     gvEvents.PageIndex = e.NewPageIndex
     LoadData()
     gvEvents.DataBind()
     // The below line refreshes the update panel..  
     AJAXPanel.Update()
end sub

Just update the AJAX Panel after databind().

assume id of the Update Panel is AJAXPanel

Protected Sub gvEvents_PageIndexChanging(...) Handles gvEvents.PageIndexChanging             
     gvEvents.PageIndex = e.NewPageIndex
     LoadData()
     gvEvents.DataBind()
     // The below line refreshes the update panel..  
     AJAXPanel.Update()
end sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文