欢迎光临
我们一直在努力

使用Repeater模板 二-ASP教程,ASP应用

建站超值云服务器,限时71元/月

asp.net提供的repeater模板并不带有分页功能,如果是少量数据的话利用repeater模板来实现分页还是不错的,毕竟repeater模板较为灵活。

<%@ 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="宋体">

<asp:repeater id="repeater1" runat="server">

<headertemplate>

<table border="0">

<tr>

<td><font color="highlight">编号</font></td>

<td><font color="highlight">名称</font></td>

<td><font color="highlight">描述</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

{

/**//// <summary>

/// webform1 的摘要说明。

/// </summary>

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.99;database=northwind;user id=sa;password=jabby";

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 窗体设计器生成的代码#region web 窗体设计器生成的代码

override protected void oninit(eventargs e)

{

//

// codegen: 该调用是 asp.net web 窗体设计器所必需的。

//

initializecomponent();

base.oninit(e);

}

/**//// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

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("connect to database error! "+sqlexp.message);

}

da = new sqldataadapter(sql_query,con);

ds = new dataset();

try

{

da.fill(ds,start,end,"categories");

this.repeater1.datasource = ds;

this.repeater1.databind();

}

catch(exception exp)

{

response.write(exp.message);

}

finally

{

con.close();

}

}

private void repeater1_itemcommand(object sender,system.web.ui.webcontrols.repeatercommandeventargs e)

{

if(e.commandname.equals("next"))

{

binddata(4,7);

}

else if(e.commandname.equals("previous"))

{

binddata(0,4);

}

}

}

}

此分页利用的是dataadapter的fill功能,但它提供的这种方式效率较低,因此只适用于少量数据,在大量数据情况下效率明显降低。 截图: 编号 名称 描述

1 beverages soft drinks, coffees, teas, beers, and ales

2 condiments sweet and savory sauces, relishes, spreads, and seasonings

3 confections desserts, candies, and sweet breads

4 dairy products cheeses

编号 名称 描述

5 grains/cereals breads, crackers, pasta, and cereal

6 meat/poultry prepared meats

7 produce dried fruit and bean curd

8 seafood seaweed and fish

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用Repeater模板 二-ASP教程,ASP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章