The "select" Operation in the gridview control is purely a UI concept. Its selectedindex attribute corresponds to the index of the currently selected row in the visible data row of the table. If
You have enabled the paging and sorting functions of tables. After performing the paging or sorting operation, the value of selectedindex remains unchanged. Therefore, after performing these operations, a new data row is selected.
. In some environments, it is better to retain the selected data row, even if the row is not visible on the current page of the table. The following example demonstrates how to retain the selected data after sorting and paging operations.
Line.
Sub gridviewdomaindatabound (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 'Check which row is selected and reselect it. 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 gridviewinclusorting (byval sender as object, byval e as system. Web. UI. webcontrols. gridviewsorteventargs) 'Reset the selected index Gridview1.selectedindex =-1 End sub |
Gridview and detailsview also support a special mode for paging and sorting, which is obtained by the client's callback (callback) operation to the server.
Data on the new page or recently sorted data. To activate this feature, you must set the enablesortingandpagingcallbacks attribute to true. Please note that when we
When performing a paging or sorting operation, the page does not need to be sent back (PostBack) to retrieve the new value (despite executing the callback operation from the client script to the server ). When the gridview or
This feature is not supported when detailsview contains template fields. When this feature is activated, you cannot display "select" in commandfield (command field)
Button.
<Asp: gridview Allowpaging = "true" allowsorting = "true" autogeneratecolumns = "false" Datakeynames = "au_id" performanceid = "sqlperformance1" Enablesortingandpagingcallbacks = "true" id = "gridview1" runat = "server"> ...... </ASP: gridview> |
Although the gridview, detailsview, and formview provide the default display mode for the pager UI, you can still set
The pagertemplate attribute defines the display of the page splitter. In this template, you can place a button control and set its commandname attribute to page
Is set to first, Prev, next, last, or <number>, where <number> is the index value of a specific page. The following example demonstrates the difference
Pagertemplate defined by the detailsview control.
commandname = "page" commandargument = "first" id = "linkbutton1"
runat = "server" style = "color: White"> & lt; & lt;
first
commandargument = "Prev" id = "linkbutton2" runat = "server" style = "color:
White"> & lt; prev
[records <% =
gridview1.pageindex * indexes %>-<% = gridview1.pageindex *
gridview1.pagesize + gridview1.pagesize-1%>]
commandname = "page" commandargument = "Next" id = "linkbutton3"
runat = "server" style = "color: White"> next & gt;
commandname = "page" commandargument = "last" id = "linkbutton4"
runat = "server" style = "color: white "> last
& gt;