ASP.net 2.0 advanced Paging/sorting

Source: Internet
Author: User
Tags prev reset sort
Asp.net| Page | advanced | sort | The select action in the data GridView control is purely a UI concept, and its SelectedIndex property corresponds to the index of the currently selected row in the table's visual data row. 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 <number>, where <number> 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= "a" id= "LinkButton1" runat= "Server" style= "Color:white" >< < a </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.PageSi Ze-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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.