Data paging in ASP.NET (Demo + Code)

xiaoxiao2021-03-06  40

Demo Address: Click ME! Paging method is very simple, it is also very efficient. Code:

int

Pages

=

5

;

int

PageSize

=

Seduce

OLEDBCONNECTION CONN

=

New

OLEDBCONNECTION

"

Provider = Microsoft.jet.Oledb.4.0; data source =

"

HttpContext.current.server.mappath (

"

Northwind.mdb

"

));

Void

Page_load

Object

Sender, Eventargs E)

{If (! Ispostback) {dgbind (getsqlstring (1));} int firstpage = int.parse (hidfirstpage.value); pnlbuttons.controls.add (getpager (firstpage));}

String

Getsqlstring

int

SELECTEDPAGE

{String [] IdSstrarray; String SQLSTR; IF (session ["IDSTR"]! = Null) {idsstrarray = session ["idstr"]. Tostring (). Split ('#');} else {sqlstr = "SELECT [ Product ID] from [Product] ORDER BY [Supplier ID] "; OLEDBDataAdapter Da = New OLEDBDataAdapter (Sqlstr, Conn); DataTable DT = New DataTable (); conn.open (); da.fill (dt); conn. Close (); da.dispose (); stringbuilder sb = new stringbuilder (""); for (int i = 0; i

int

Firstpage)

{INT pageCount = int.parse (hidpagecount.value); Panel pnlpager = new panel (); pnlpager.controls.add (new literalcontrol ("
total" pageCount.toString () "page"); pnlpager .Controls.add (GetLinkButton ("FST", "Home", "PGBTN", "1")); PNLPager.Controls.Add (New LitralControl (")); if (FirstPage> 0) {PNLPager.Controls. Add ("pre", "front" pages.tostring () "PGBTN", (FirstPage - Pages 1); pnlpager.controls.add (New LitralControl) "));} For (int i = firstpage 1; i pagecount) {Break;} pnlpager.controls.add (getLinkButton (I.toToString ()," [" I.tostring () "] "," pgbtn ", I.toTOString ())); pnlPager.controls.add (new literalcontrol ("));} if (firstpage pages

String

LBID,

String

LbText,

String

lbcmdname,

String

lbcmdarg)

{LinkButton lb = new LinkButton (); lb.ID = lbCmdName lbId; lb.Text = lbText; lb.CommandName = lbCmdName; lb.CommandArgument = lbCmdArg; lb.Command = new CommandEventHandler (pageTurn); return lb;} Void

PageTurn

Object

Sender, CommandEventArgs E)

{LinkButton lb = (LinkButton) sender; int selectedPage = int.Parse (lb.CommandArgument); int firstPage = selectedPage / pages * pages; firstPage = (selectedPage% pages == 0) (firstPage - pages):? FirstPage; hidFirstPage .Value = firstPage.ToString (); dgBind (getSqlString (selectedPage)); pnlButtons.Controls.RemoveAt (pnlButtons.Controls.Count - 1); pnlButtons.Controls.Add (getPager (firstPage)); lb = (LinkButton) pnlButtons .Controls [pnlbuttons.controls.count - 1] .FindControl ("pgbtn" selectedPage.tostring ()); lb.enabled = false;}

Void

DGBind

String

SQLSTR)

{OLEDBDataAdapter Da = New OLEDBDataAdapter (SQLSTR, Conn); DataBole DT = New DataTable (); conn.open (); da.fill (dt); conn.close (); da.dispose (); DG.DataSource = DT DG.DATABIND ();

Void

DG_ItemDatabase

Object

Sender, DataGriditeMeventArgs E)

{DG.Controls [0] .enableviewState = false;

<%

@ Import namespace = "system.text"

%>

<%

@ Import namespace = "system.data.oledb"

%>

<%

@ Import namespace = "system.data"

%>

<%

@ Page language = "c #"

%>

DOCTYPE HTML PUBLIC "- // w3c // DTD XHTML 1.1 // en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">

<

HTML

>

<

HEAD

>

<

Title

>

Dynamic Buttons

Title

>

<

Script

Runat

= "Server"

>

// Insert the C # code above

Script

>

<

Style

>

Body {} {font-size: 9pt; font-family: Tahoma, Song; Text-align: center} TD {} {font-size: 9pt; font-family: Tahoma, Song; Text-align: center}

Style

>

HEAD

>

<

Body

>

<

FORM

id

= "Form1"

Runat

= "Server"

>

<

Div

>

<

ASP: Label

id

= "LBLPROMPT"

Runat

= "Server"

TEXT

= "?"

EnableViewState

= "False"

enabletheming

= "True"

>

ASP: Label

>

<

Br

>

<

Br

>

<

Br

>

<

ASP: PANEL

id

= "pnlbuttons"

Runat

= "Server"

Width

= "560px"

>

<

INPUT

id

= "hidfirstpage"

Type

= "hidden"

Value

= "0"

Runat

= "Server"

> <

INPUT

id

= "HIDPAGECOUNT"

Type

= "hidden"

Runat

= "Server"

>

<

ASP: DATAGRID

id

= "DG"

Runat

= "Server"

Width

= "100%"

PageSize

= "5"

AutogenerateColumns

= "False"

Bordercolor

= "# Cc9966"

Borderstyle

= "None"

Borderwidth

= "1px"

Backcolor

= "White"

Cellpadding

= "4"

OntemDatabase

= "DG_ItemDatabase"

>

<

Footerstyle

Forecolor

= "# 330099"

Backcolor

= "# Fffcc"

>

Footerstyle

>

<

HEADERSTYLE

Font-bold = "True"

Forecolor

= "# Fffcc"

Backcolor

= "# 990000"

>

HEADERSTYLE

>

<

Pagerstyle

Horizontalalign

= "Center"

Forecolor

= "# 330099"

Backcolor

= "# Fffcc"

>

Pagerstyle

>

<

SelectEdItemStyle

Font-bold

= "True"

Forecolor

= "# 663399"

Backcolor

= "# Ffcc66"

>

SelectEdItemStyle

>

<

ItemStyle

Forecolor

= "# 330099"

Backcolor

= "White"

>

ItemStyle

>

<

Columns

>

<

ASP: BoundColumn

Datafield

= "Product ID"

Headertext

= "Product ID"

>

<

HEADERSTYLE

Width

= "60px"

>

HEADERSTYLE

>

ASP: BoundColumn

>

<

ASP: BoundColumn

Datafield

= "Product Name"

Headertext

= "Product Name"

>

ASP: BoundColumn

>

<

ASP: BoundColumn

Datafield

= "Company Name"

Headertext

= "Supplier"

>

<

HEADERSTYLE

Width

= "80px"

>

HEADERSTYLE

>

ASP: BoundColumn

>

<

ASP: BoundColumn

Datafield

= "Category Name"

Headertext

= "Category"

>

<

HEADERSTYLE

Width

= "100px"

>

HEADERSTYLE

>

ASP: BoundColumn

>

Columns

>

ASP: DATAGRID

>

ASP: PANEL

>

Div

>

<

Br

>

<

Br

>

<

Br

>

<

ASP: HyperLink

id

= "lnkreeload"

Runat

= "Server"

Width

= "80px"

NavigateURL

= "Pager.aspx"

EnableViewState

= "False"

>

Overload

ASP: HyperLink

>

FORM

>

Body

>

HTML

>

转载请注明原文地址:https://www.9cbs.com/read-118479.html

New Post(0)