7 vb.net asp.net paging gridview updatepanel
我有一个最初绑定到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()
Run Code Online (Sandbox Code Playgroud)
这非常有效,直到我添加了一个AJAX更新面板,因此每次分页时整个页面都不会回发,并且分页停止工作.我调试它并发现它实际上正在调用PageIndexChanging事件,但什么也没发生.
我搜索了网络,发现一些人有同样的问题,但他们的解决方案对我不起作用.这个网站上有一个问题通过以下方式解决:
在PageIndexchanging事件中,将数据绑定到网格,确保再次从数据库中提取数据
我不知道这意味着什么; 如上所示,我的数据受到约束.我将"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>
Run Code Online (Sandbox Code Playgroud)
在 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22824 次 |
| 最近记录: |