在AJAX updatepanel中分页不能在asp.net gridview中工作

前端之家收集整理的这篇文章主要介绍了在AJAX updatepanel中分页不能在asp.net gridview中工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个最初绑定到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事件,但没有发生任何事情.

搜索了网络,发现一些人有同样的问题,但他们的解决方案对我不起作用.这个网站上有一个问题通过以下方式解决

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

我不知道这意味着什么;如上所示,我的数据受到约束.我将“enable paging”设置为true,将EnableSortingAndPagingCallbacks设置为false.

如果有人可以帮助我,我真的很感激.我在下面的updatepanel中包含了我的标记.非常感谢!

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

In PageIndexchanging event,data is again fetched from the DB
I don’t know what that means; my data was being bound as demonstrated above.

这意味着您需要在页面后面的代码中再次获取数据.您正在设计/ 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

猜你在找的Ajax相关文章