下面是显示sql数据库中的内容,显示信息和图片,每行显示3个。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 hysm { /// <summary> /// yytd 的摘要说明。 /// </summary> public class yytd : System.Web.UI.Page { protected System.Web.UI.WebControls.Table Table1; protected System.Data.SqlClient.SqlConnection sqlConnection1; protected System.Data.SqlClient.SqlCommand sqlCommand1; private const int step = 3; private int rowcount;//用来存储院产品表数据表中的记录数 private long TabRowCount; protected dbc myDBConnection; protected string str_lbid; private string str_cpmc,str_cpjs,str_xpicurl,str_dpicurl,str_sql; protected string str_picpath = "yytd/";//图片的存放路径
} // } } dr.Close(); this.myDBConnection.DbClose(this.sqlConnection1); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlCommand1 = new System.Data.SqlClient.SqlCommand(); // // sqlCommand1 // this.sqlCommand1.Connection = this.sqlConnection1; this.Load += new System.EventHandler(this.Page_Load); } #endregion
/// <summary> /// 动态计算生成表格行数的方法,参数rowcount为总共要显示的记录数,tablecolumnnum为生成的表格的列数; /// rowcout为方法的返回值,是计算出来的一共要生成的行数。 ///调用方法如下 例:tabrow = CalcRow(rowcounnt,tablecolumnnum) /// </summary> public long CalcRow(long rownum, int tablecolumnnum) { int temp; long tablerownum; temp = System.Convert.ToInt32(rownum%tablecolumnnum); if (temp == 0) tablerownum = rownum/tablecolumnnum; else tablerownum = System.Convert.ToInt32(rownum/tablecolumnnum) + 1; return tablerownum; } } }
给个代码看看?可以试试用SQL语句分页
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 hysm
{
/// <summary>
/// yytd 的摘要说明。
/// </summary>
public class yytd : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlCommand sqlCommand1;
private const int step = 3;
private int rowcount;//用来存储院产品表数据表中的记录数
private long TabRowCount;
protected dbc myDBConnection;
protected string str_lbid;
private string str_cpmc,str_cpjs,str_xpicurl,str_dpicurl,str_sql;
protected string str_picpath = "yytd/";//图片的存放路径
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//this.str_lbid = Request.QueryString["LBID"]; this.str_sql = "select top 9 * from CPXX ";//构造SQL语句,用于检索在研产品数据
this.myDBConnection = new dbc();
this.myDBConnection.DbConnect(this.sqlConnection1);//打开数据库连接
this.sqlCommand1.CommandText = "select count(*) from CPXX";
string str_temp;
str_temp = this.sqlCommand1.ExecuteScalar().ToString().Trim();
if(str_temp == "")
{
rowcount = 0;
}
else
{
rowcount = System.Convert.ToInt32(sqlCommand1.ExecuteScalar());
} TabRowCount = CalcRow(rowcount,step);//计算需要显示多少行
this.sqlCommand1.CommandText = this.str_sql;
SqlDataReader dr = sqlCommand1.ExecuteReader();//执行查询操作
for (int i=1;i<=TabRowCount;i++) //
{ //
int j = 0; //
TableRow xpicurlRow = new TableRow();//产品名称行 //
TableRow cpmcRow = new TableRow();//产品名称行 //
TableRow cpjsRow = new TableRow();//产品简介行
for(j=1;j<=step;j++) //
{ //
//
if (dr.Read()) //
{ //
this.str_cpmc = System.Convert.ToString(dr["cpmc"]).Trim(); //
this.str_cpjs = System.Convert.ToString(dr["cpjs"]).Trim(); //
this.str_xpicurl = System.Convert.ToString(dr["xpicurl"]).Trim();
this.str_dpicurl = System.Convert.ToString(dr["dpicurl"]).Trim();
TableCell cpmc = new TableCell(); //
cpmc.BorderWidth = 1; //
cpmc.Height = 20;
cpmc.BackColor = System.Drawing.Color.Blue;
cpmc.ForeColor = System.Drawing.Color.White;
cpmc.Width = 207;
cpmc.HorizontalAlign = HorizontalAlign.Center; //
cpmc.Controls.Add(new LiteralControl(this.str_cpmc));//插入产品名称 //
//cpmcRow.Cells.Add(cpmc);//添加产品名称表格单元 //
this.str_cpjs = System.Convert.ToString(dr["cpjs"]).Trim();
TableCell cpjs = new TableCell(); //
cpjs.BorderWidth = 1; //
cpjs.Width = 207;
cpjs.HorizontalAlign = HorizontalAlign.Left; //
cpjs.VerticalAlign = VerticalAlign.Top; //
cpjs.Controls.Add(new LiteralControl(this.str_cpjs));
//cpjsRow.Cells.Add(cpjs);//添加产品简介表格单元 TableCell xpicurl = new TableCell(); //
xpicurl.BorderWidth = 1; //
xpicurl.Width = 207;
xpicurl.HorizontalAlign = HorizontalAlign.Center; //
if(this.str_xpicurl != "") //
{ //
xpicurl.Controls.Add(new LiteralControl("<A href=" + this.str_picpath + this.str_dpicurl + //
" target=_blank>" + "<IMG src = " + this.str_picpath + this.str_xpicurl + " border=0>" + "</A>")); //
} //
else //
{ //
this.str_picpath = "home.jpg"; //
xpicurl.Controls.Add(new LiteralControl("<IMG src = " + this.str_xpicurl + ">")); //
}
xpicurlRow.Cells.Add(xpicurl); //
//
//
xpicurlRow.Height = 150;
Table1.Rows.Add(xpicurlRow); //
Table1.CellSpacing = 0;
//Table1.Rows.Add(cpmcRow); //
Table1.CellSpacing = 0;
Table1.Rows.Add(cpjsRow); //
Table1.CellSpacing = 0;
} //
}
}
dr.Close();
this.myDBConnection.DbClose(this.sqlConnection1);
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
//
// sqlCommand1
//
this.sqlCommand1.Connection = this.sqlConnection1;
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
/// <summary>
/// 动态计算生成表格行数的方法,参数rowcount为总共要显示的记录数,tablecolumnnum为生成的表格的列数;
/// rowcout为方法的返回值,是计算出来的一共要生成的行数。
///调用方法如下 例:tabrow = CalcRow(rowcounnt,tablecolumnnum)
/// </summary>
public long CalcRow(long rownum, int tablecolumnnum)
{
int temp;
long tablerownum;
temp = System.Convert.ToInt32(rownum%tablecolumnnum);
if (temp == 0)
tablerownum = rownum/tablecolumnnum;
else
tablerownum = System.Convert.ToInt32(rownum/tablecolumnnum) + 1;
return tablerownum;
}
}
}
给个DEMO出来让大家看看嘛
http://pic.sina.com.cn/cgi-bin/retr/search?query=内衣
asp的服务器控件中的table不可以分页,不过你可以使用变通的办法,让它绑定的数据源比如datatable这种collection的容器,这样设置一个固定大小用于每页显示记录数量,然后通过集合的索引来获得每页的数据并绑定到table中。