Use the Repeater template two

xiaoxiao2021-03-06  94

The Repeater template provided by ASP.NET does not have paging features. If you are a small amount of data, you can use the REPEATER template to implement paging or good, after all, the Repeater template is more flexible.

<%

@ Page language = "c #" codebehind = "Webform1.aspx.cs" autoeventwireup = "false" inherits = "WebApplication1.webform1"

%>

DOCTYPE HTML PUBLIC "- // w3c // DTD HTML 4.0 Transitional // En"

>

<

HTML

>

<

HEAD

>

<

Title

>

Webform1

Title

>

<

Meta

Name

= "Generator"

Content

= "Microsoft Visual Studio .NET 7.1"

>

<

Meta

Name

= "Code_Language"

Content

= "C #"

>

<

Meta

Name

= "VS_DEFAULTCLIENTScript"

Content

= "JavaScript"

>

<

Meta

Name

= "vs_targetschema"

Content

= "http://schemas.microsoft.com/intellisense/ie5"

>

HEAD

>

<

Body

MS_Positioning

= "GridLayout"

>

<

FORM

id

= "Form1"

Method

= "POST"

Runat

= "Server"

>

<

Font

Face

= "Song"

>

<

ASP: REPEATER

id

= "REPEATER1"

Runat

= "Server"

>

<

Headertemplate

>

<

TABLE

Border

= "0"

>

<

TR

>

<

TD

> <

Font

Color

= "Highlight"

>

serial number

Font

>

TD

>

<

TD

> <

Font

Color

= "Highlight"

>

name

Font

>

TD

>

<

TD

> <

Font

Color

= "Highlight"

>

description

Font

>

TD

>

TR

>

Headertemplate

>

<

ItemTemplate

>

<

TR

Bgcolor

= "LIGHTYELLOW"

>

<

TD

>

<%

# DataBinder.eval (Container.DataItem, "CategoryID")%>

TD

>

<

TD

>

<%

# DataBinder.eval (Container.DataItem, "categoryName")

%>

TD

>

<

TD

>

<%

# DataBinder.eval (Container.DataItem, "Description")

%>

TD

>

TR

>

ItemTemplate

>

<

AlternatingItemTemplate

>

<

TR

>

<

TD

>

<%

# DataBinder.eval (Container.DataItem, "categoryid")

%>

TD

>

<

TD

>

<%

# DataBinder.eval (Container.DataItem, "categoryName")

%>

TD

>

<

TD

>

<%

# DataBinder.eval (Container.DataItem, "Description")

%>

TD

>

TR

>

AlternatingItemTemplate

>

<

Footertemplate

>

TABLE

>

<

ASP: ImageButton

CommandName

= "previous"

Id

= "previous"

ImageURL

= "./ images / previous.gif"

Runat

= "Server"

/>

<

ASP: ImageButton

CommandName

= "Next"

Id

= "Next"

ImageURL

= "(iMages / next.gif"

Runat

= "Server"

/>

Footertemplate

>

ASP: REPEATER

>

Font

>

FORM

>

Body

>

HTML

>

Using

System;

Using

System.collections;

Using

System.componentmodel;

Using

System.data;

Using

System.data.sqlclient;

Using

System.drawing;

Using

System.Web;

Using

System.Web.SessionState;

Using

System.Web.ui;

Using

System.Web.ui.WebControls;

Using

System.Web.ui.htmlControls;

Namespace

WebApplication1

{/ ** ////

/// WebForm1 summary description. /// public class WebForm1: System.Web.UI.Page {protected System.Web.UI.WebControls.Repeater Repeater1; protected SqlConnection con = null; protected string CON_STR = "server = 192.168.0.XX; database = Northwind; user id = sa; password = XXX "; protected string sQL_QUERY =" select * from Categories "; protected SqlDataAdapter da = null; protected DataSet ds = null; private void Page_Load (object sender, System.EventArgs e) { If (! page.ispostback) {binddata (0, 4);}} Web Form Designer Generated Code #Region Web Form Designer Generated Code Override Protected Void OnInit (Eventargs E) {/// Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.oninit (e);} / ** //// /// Designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.Repeater1.ItemCommand = new System.Web.UI.WebControls.RepeaterCommandEventHandler (this.Repeater1_ItemCommand); this.Load = new System.EventHandler (this.Page_Load) ;} #endregion private void BindData (int start, int end) {try {con = new SqlConnection (CON_STR);} catch (SqlException sqlexp) {Response.Write ( sqlexp.Message "Connect to database Error!");} DA = New SqldataAdapter (SQL_QUERY, CON); DS = New DataSet (); try {da.fill (DS, START, END, "categories"); this.repeater1.datasource = DS; this.repeater1.database (); } CatCH (Exception Exp) {response.write (exp.Message);} finally {conif. );}}} Private void repeater1_itemCommand (Object sender, system.web.ui.webcontrols.repeatermanCommandEventAndargs e) {if (e.commandname.equals ("next")) {binddata (4, 7);} else if (e. CommandName.Equals ("previous")) {BindData (0, 4);}}}} This page utilizes the DataAdapter's Fill function, but this way is highly efficient, so it only applies to a small amount of data, Large amount of data is significantly reduced.

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

New Post(0)