A combination of DataGrid and stored procedures that reads only the current page data

Source: Internet
Author: User
datagrid| stored Procedures | pagination | data

<%@ Import namespace= "System.Data.SqlClient"%>
<%@ Import namespace= "System.Data"%>

<script runat= "Server" >

Dim Connorthwind as SqlConnection
Dim Strselect as String
Dim Intstartindex as Integer
Dim Intendindex as Integer

Sub Page_Load
Dim Cmdselect as SqlCommand
Btnfirst.text = "Home"
Btnprev.text = "Previous Page"
Btnnext.text = "Next Page"
Btnlast.text = "Last Page"
Connorthwind = New SqlConnection ("server=192.168.4.1; Uid=sa; Pwd=xxxxxx;database=northwind ")
If not IsPostBack Then
' Get Total Pages
Strselect = "Select COUNT (OrderID) from Orders"
Cmdselect = New SqlCommand (strselect, Connorthwind)
Connorthwind.open ()
Dgrdproducts.virtualitemcount = (Cmdselect.executescalar ()/dgrdproducts.pagesize)
Connorthwind.close ()
Binddatagrid
End If
End Sub

Sub Binddatagrid
Dim Dadproducts as SqlDataAdapter
Dim Dstproducts as DataSet

Intendindex = Dgrdproducts.pagesize
Dadproducts = New SqlDataAdapter ("orderspaged", Connorthwind)
DadProducts.SelectCommand.CommandType = CommandType.StoredProcedure
DADPRODUCTS.SELECTCOMMAND.PARAMETERS.ADD ("@PageIndex", Intstartindex)
DADPRODUCTS.SELECTCOMMAND.PARAMETERS.ADD ("@PageSize", Intendindex)
Dstproducts = New DataSet
Dadproducts.fill (dstproducts)

Dgrdproducts.datasource = dstproducts
Dgrdproducts.databind ()
End Sub

Sub dgrdproducts_pageindexchanged (s as Object, E as DataGridPageChangedEventArgs)
Intstartindex = E.newpageindex
Dgrdproducts.currentpageindex = E.newpageindex
Binddatagrid
End Sub
Sub Pagerbuttonclick (ByVal sender as Object, ByVal e as EventArgs)
Dim arg as String = sender.commandargument
Select Case Arg
Case "Next"
If (Dgrdproducts.currentpageindex < (dgrdproducts.pagecount-1)) Then
Dgrdproducts.currentpageindex + 1
End If
Case "Prev"
If (Dgrdproducts.currentpageindex > 0) Then
Dgrdproducts.currentpageindex-= 1
End If
Case "Last"
Dgrdproducts.currentpageindex = (dgrdproducts.pagecount-1)
Case Else
' Page number
Dgrdproducts.currentpageindex = System.Convert.ToInt32 (ARG)
End Select
Intstartindex=dgrdproducts.currentpageindex
Binddatagrid
End Sub
</Script>

<body>
<form runat= "Server" >

<asp:datagrid runat= "Server"
Id= "Dgrdproducts"
Showheader= "false"
Allowpaging= "True"
Allowcustompaging= "True"
Pagesize= "10"
Onpageindexchanged= "Dgrdproducts_pageindexchanged"
Pagerstyle-mode= "NumericPages"
Alternatingitemstyle-backcolor= "#eeaaee"
Headerstyle-backcolor= "#aaFFdd"
Font-size= "10pt"
Font-name= "Verdana"
cellspacing= "0"
Cellpadding= "3"
Gridlines= "Both"
Borderwidth= "1"
Bordercolor= "BLACK"
Pagerstyle-horizontalalign= "Right" >
<alternatingitemstyle backcolor= "#EEEEEE" ></AlternatingItemStyle>
</asp:datagrid>
<asp:linkbutton id= "Btnfirst" runat= "server" font-name= "Verdana" font-size= "8pt" forecolor= "Navy" CommandArgument = "0" ></asp:linkbutton>
<asp:linkbutton id= "Btnprev" runat= "server" font-name= "Verdana" font-size= "8pt" forecolor= "Navy" commandargument= "Prev" ></asp:linkbutton>
<asp:linkbutton id= "Btnnext" runat= "server" font-name= "Verdana" font-size= "8pt" forecolor= "Navy" commandargument= "Next" ></asp:linkbutton>
<asp:linkbutton id= "Btnlast" runat= "server" font-name= "Verdana" font-size= "8pt" forecolor= "Navy" commandargument= "Last" ></asp:linkbutton>
</form>
The following are stored procedures:
CREATE PROCEDURE orderspaged
(
&nb



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.