The select action in the GridView control is purely a UI concept, and its SelectedIndex property corresponds to the index of the currently selected row in the visible data row of the table. If you enable the paging and sorting function of the table, the value of the SelectedIndex will not change after the paging or sorting operation, so a new row of data is selected after performing these operations. In some environments, preserving the selection of the specified data row is better, even if the row is not visible in the current page of the table. The following example shows how to retain the currently selected data row after the sorting and paging operation.
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
' Detect which selected row is and select it again
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)
' Save the index of the selected row
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)
' Reset the selected index
Gridview1.selectedindex =-1
End Sub
Sub gridview1_sorting (ByVal sender as Object, ByVal e as System.Web.UI.WebControls.GridViewSortEventArgs)
' Reset Selection Index
Gridview1.selectedindex =-1
End Sub
The GridView and DetailsView also support a special pattern for paging and sorting, which takes advantage of client-side callback (callback) operations to obtain data from new pages or recently sorted data. To activate this feature, you must set the EnableSortingAndPagingCallbacks property to True. Note that when we perform a paging or sort operation, the page does not need to be sent back (postback) to retrieve the new value (although client script callback to the server is performed). This attribute is not supported when the GridView or DetailsView contains template fields. When this feature is activated, the "Select" button is not supported in CommandField (command field).
<asp:GridView AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="SqlDataSource1" EnableSortingAndPagingCallbacks="True" ID="GridView1" runat="server">
……
</asp:GridView>
Although the GridView, DetailsView, and FormView provide the default display for the pager (pager) UI, you can still customize the display of the pager by setting the PagerTemplate property. In this template, you can place the button control and set its CommandName property to page, setting its properties to the top, Prev, Next, last, or , where is the index value for a particular page. The following example shows the PagerTemplate defined by the GridView and DetailsView controls.
<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>