Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Row As GridViewRow
Dim SelectedValue As String = ViewState("SelectedValue")
If SelectedValue Is Nothing Then
Return
End If
' 检测选择的行是哪个并重新选中它
For Each Row In GridView1.Rows
Dim KeyValue As String = GridView1.DataKeys(Row.RowIndex).Value
If (KeyValue = SelectedValue) Then
GridView1.SelectedIndex = Row.RowIndex
End If
Next
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
' 保存选中行的索引
If (Not GridView1.SelectedIndex = -1) Then
ViewState("SelectedValue") = GridView1.SelectedValue
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
' 重置选中索引
GridView1.SelectedIndex = -1
End Sub
Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
' 重置选择索引
GridView1.SelectedIndex = -1
End Sub
<asp:GridView AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="SqlDataSource1" EnableSortingAndPagingCallbacks="True" ID="GridView1" runat="server">
……
</asp:GridView>
<PagerTemplate>
<asp:LinkButton CommandName="Page" CommandArgument="First" ID="LinkButton1" runat="server" Style="color: white"><< First</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="LinkButton2" runat="server" Style="color: white">< Prev</asp:LinkButton>
[Records <%= GridView1.PageIndex * GridView1.PageSize %> - <%= GridView1.PageIndex * GridView1.PageSize + GridView1.PageSize - 1 %>]
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="LinkButton3" runat="server" Style="color: white">Next ></asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="LinkButton4" runat="server" Style="color: white">Last >></asp:LinkButton>
</PagerTemplate>