2008-03-08

GridView Page Ajax History 換頁時加入歷史記錄

在上一篇文章介紹過如何如用ASP.NET 3.5 Extendsion的History功能今天就來實做GridView結合ObjectDataSource或SqlDataSource的換頁瀏覽紀錄

前端.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server" EnableHistory="true" EnableStateHash="false"/>
<asp:TextBox ID="TextBox1" runat="server" /><asp:Button ID="Button1" runat="server" Text="Button" />
<asp:GridView ID="GridView_CustomPaging" runat="server" AllowPaging="true"
AllowSorting="true" AutoGenerateColumns="False" DataKeyNames="StuWeekID" DataSourceID="ObjectDataSource_CustomPaging" SkinID="TaideGridView">
<Columns>
<asp:BoundField DataField="StuUserID" HeaderText="StuUserID" SortExpression="StuUserID" />
<asp:BoundField DataField="EntID" HeaderText="EntID" SortExpression="EntID" />
<asp:BoundField DataField="SchoolID" HeaderText="SchoolID" SortExpression="SchoolID" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource_CustomPaging" runat="server" SelectMethod="GetStuWeekData" TypeName="mgStuWeek" EnablePaging="True"/>"

後端.aspx.vb(寫c#的朋友拍謝啦!網路上有很多轉換的網站,我就不貼上來了)
Protected Sub GridView_CustomPaging_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView_CustomPaging.PageIndexChanged
'換頁時把頁碼加入歷史紀錄
If ScriptManager1.IsInAsyncPostBack And Not ScriptManager1.IsNavigating Then
Dim index As Integer = GridView_CustomPaging.PageIndex
ScriptManager1.AddHistoryPoint("PageIndex", index, "第 " & index + 1 & " 頁")
End If
End Sub

Protected Sub ScriptManager1_Navigate(ByVal sender As Object, ByVal e As System.Web.UI.HistoryEventArgs) Handles ScriptManager1.Navigate
'按上一頁會進到歷史紀錄的功能
Dim indexString As String = e.State("PageIndex")
Page.Title = "第 " & indexString & " 頁"
GridView_CustomPaging.PageIndex = CType(indexString, Integer)
GridView_CustomPaging.DataSourceID = ObjectDataSource_CustomPaging.ID
End Sub

搞定,簡單吧!

0 comments: