[导入]DATA LIST做做产品显示加分页
以前做asp.net的时候做过一个产品展示的小东西..因为是产品展示.产品一般都要求一行几个的排列,DataGrid,DataList这些都没有这么细看过,用起来也不熟识,而且我一向都不怎么喜欢用DataGrid,甚至有点讨厌DataGrid,所以那次做的产品展示.我基本上全部都是使用DataReader,DataReader的用法和JSP里的ResultSet很是相似(MS很多都是抄人家的嘛..呵呵^_^),因为用了DataReader,分页就成了一个问题,还好做过一个JSP的分页,用的就是ResultSet. 基本代码如下:
呵呵,虽然我也知道这样写其实很笨...但是时间比较紧...没法子了..写出来才算了...完成之后本来想研究一下有没有什么方法可以改进的,,,但是却不了了之了.
这些天闲着没事..看了一下MSDN上的DataList,,居然DataList就提供了一个不错的方法来解决这个这个产品的展示格式的问题,<AlternatingItemTemplate>
虽然这样看MSDN上提供的备注不是很清楚...但是测试过就知道了.
至于DataList的分页,网上的例子很多,不再展示,自己去Google一下吧.
文章来源:http://www.blogjava.net/TtTiCk/archive/2006/02/01/29445.html
protected System.Web.UI.HtmlControls.HtmlGenericControl mydata;
protected System.Web.UI.HtmlControls.HtmlGenericControl pagedata;
OleDbConnection conn= new OleDbConnection() ;
private void Page_Load(object sender, System.EventArgs e)
{
string MyConnString=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
MyConnString+=Server.MapPath("showpic_data\\show_pic_tttick.mdb");
conn.ConnectionString=MyConnString;
int intRowCount;
int intPageCount;
int intPage;
String strPage;
int i,j;
int intPageSize = 16;
strPage = Request.Params["page"];
if(strPage==null)
{
intPage = 1;
}
else
{
intPage = Convert.ToInt32(strPage);
if(intPage<1) intPage = 1; }
intRowCount=CalculateRecord() ;
intPageCount= (intRowCount+intPageSize-1) / intPageSize;
i = (intPage-1) * intPageSize;
string ConnStr="select * from show_pic_pic where show_pic_classid like 2";
conn.Open();
OleDbCommand cmd=new OleDbCommand(ConnStr,conn);
OleDbDataReader myReader = cmd.ExecuteReader();
for(j=0;j<i;j++) myReader.Read();
i = 0;
int zz=0;
string html="<table border='0 'cellspacing='0' cellpadding='0'><tr>";
string delid;
while( i<intPageSize && myReader.Read())
{
delid=myReader["id"].ToString().Trim();
html+="<td width='110'><table width='110' border='0' cellpadding='0' cellspacing='0'><tr><td><a href=\"javascript:void(0)\" onClick=\"MM_openBrWindow('showpic.aspx?id="+myReader["id"].ToString().Trim()+"','','width=590,height=600,scrollbars=yes')\"><img src=\""+myReader["show_pic_picpath"].ToString().Trim().Remove(0,3)+"\" width='110' height='80'></a></td></tr><tr><td width='110'><div align=\"center\">"+myReader["show_pic_picname"].ToString()+"</div></td></tr></table></td><td width='20'></td>";
zz++;
i++;
if(zz%4==0)
html+="</tr><tr>";
}
myReader.Close();
html+="</tr></table>";
mydata.InnerHtml=html;
pagedata.InnerHtml="第"+intPage+"页 共"+intPageCount+"页 ";
if(intPage>1)
{
pagedata.InnerHtml+="<a href='product2.aspx?page="+(intPage-1)+"'>上一页</a> ";
}
if(intPage<intPageCount)
{
pagedata.InnerHtml+="<a href='product2.aspx?page="+(intPage+1)+"'>下一页</a> ";
}
// 在此处放置用户代码以初始化页面
}
protected System.Web.UI.HtmlControls.HtmlGenericControl pagedata;
OleDbConnection conn= new OleDbConnection() ;
private void Page_Load(object sender, System.EventArgs e)
{
string MyConnString=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
MyConnString+=Server.MapPath("showpic_data\\show_pic_tttick.mdb");
conn.ConnectionString=MyConnString;
int intRowCount;
int intPageCount;
int intPage;
String strPage;
int i,j;
int intPageSize = 16;
strPage = Request.Params["page"];
if(strPage==null)
{
intPage = 1;
}
else
{
intPage = Convert.ToInt32(strPage);
if(intPage<1) intPage = 1; }
intRowCount=CalculateRecord() ;
intPageCount= (intRowCount+intPageSize-1) / intPageSize;
i = (intPage-1) * intPageSize;
string ConnStr="select * from show_pic_pic where show_pic_classid like 2";
conn.Open();
OleDbCommand cmd=new OleDbCommand(ConnStr,conn);
OleDbDataReader myReader = cmd.ExecuteReader();
for(j=0;j<i;j++) myReader.Read();
i = 0;
int zz=0;
string html="<table border='0 'cellspacing='0' cellpadding='0'><tr>";
string delid;
while( i<intPageSize && myReader.Read())
{
delid=myReader["id"].ToString().Trim();
html+="<td width='110'><table width='110' border='0' cellpadding='0' cellspacing='0'><tr><td><a href=\"javascript:void(0)\" onClick=\"MM_openBrWindow('showpic.aspx?id="+myReader["id"].ToString().Trim()+"','','width=590,height=600,scrollbars=yes')\"><img src=\""+myReader["show_pic_picpath"].ToString().Trim().Remove(0,3)+"\" width='110' height='80'></a></td></tr><tr><td width='110'><div align=\"center\">"+myReader["show_pic_picname"].ToString()+"</div></td></tr></table></td><td width='20'></td>";
zz++;
i++;
if(zz%4==0)
html+="</tr><tr>";
}
myReader.Close();
html+="</tr></table>";
mydata.InnerHtml=html;
pagedata.InnerHtml="第"+intPage+"页 共"+intPageCount+"页 ";
if(intPage>1)
{
pagedata.InnerHtml+="<a href='product2.aspx?page="+(intPage-1)+"'>上一页</a> ";
}
if(intPage<intPageCount)
{
pagedata.InnerHtml+="<a href='product2.aspx?page="+(intPage+1)+"'>下一页</a> ";
}
// 在此处放置用户代码以初始化页面
}
呵呵,虽然我也知道这样写其实很笨...但是时间比较紧...没法子了..写出来才算了...完成之后本来想研究一下有没有什么方法可以改进的,,,但是却不了了之了.
这些天闲着没事..看了一下MSDN上的DataList,,居然DataList就提供了一个不错的方法来解决这个这个产品的展示格式的问题,<AlternatingItemTemplate>
备注
使用 AlternatingItemTemplate 属性来控制 DataList 控件中交替项的内容。交替项的外观由 AlternatingItemStyle 属性控制。
若要为交替项指定模板,请在 DataList 控件的开始标记和结束标记之间放置 <AlternatingItemTemplate> 标记。然后可以在开始和结束 <AlternatingItemTemplate> 标记之间列出模板的内容。
虽然这样看MSDN上提供的备注不是很清楚...但是测试过就知道了.
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<HTML>
<script runat="server">
ICollection CreateDataSource()
{
// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
dt.Columns.Add(new DataColumn("ImageValue", typeof(String)));
// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Description for item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dr[3] = "Image" + i.ToString() + ".jpg";
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
}
}
</script>
<body>
<form runat="server" ID="Form1">
<h3>DataList AlternatingItemTemplate Example</h3>
<asp:DataList id="ItemsList" BorderColor="black" CellPadding="5" CellSpacing="5" RepeatDirection="Vertical"
RepeatLayout="Table" RepeatColumns="4" ShowBorder="True" ShowFooter="True" runat="server">
<HeaderStyle BackColor="#aaaadd"></HeaderStyle>
<ItemStyle BackColor="Silver"></ItemStyle>
<AlternatingItemStyle BackColor="Silver"></AlternatingItemStyle>
<HeaderTemplate>
List of items
</HeaderTemplate>
<ItemTemplate>
Description:
<br>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
<br>
Price:
<%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>
<br>
<asp:Image id="ProductImage"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
runat="server"/>
</ItemTemplate>
<AlternatingItemTemplate>
Description:
<br>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
<br>
Price:
<%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>
<br>
<asp:Image id="Image1"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
runat="server"/>
</AlternatingItemTemplate>
</asp:DataList>
</form>
</body>
</HTML>
<%@ Page Language="C#" AutoEventWireup="True" %>
<HTML>
<script runat="server">
ICollection CreateDataSource()
{
// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
dt.Columns.Add(new DataColumn("ImageValue", typeof(String)));
// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Description for item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dr[3] = "Image" + i.ToString() + ".jpg";
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
}
}
</script>
<body>
<form runat="server" ID="Form1">
<h3>DataList AlternatingItemTemplate Example</h3>
<asp:DataList id="ItemsList" BorderColor="black" CellPadding="5" CellSpacing="5" RepeatDirection="Vertical"
RepeatLayout="Table" RepeatColumns="4" ShowBorder="True" ShowFooter="True" runat="server">
<HeaderStyle BackColor="#aaaadd"></HeaderStyle>
<ItemStyle BackColor="Silver"></ItemStyle>
<AlternatingItemStyle BackColor="Silver"></AlternatingItemStyle>
<HeaderTemplate>
List of items
</HeaderTemplate>
<ItemTemplate>
Description:
<br>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
<br>
Price:
<%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>
<br>
<asp:Image id="ProductImage"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
runat="server"/>
</ItemTemplate>
<AlternatingItemTemplate>
Description:
<br>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
<br>
Price:
<%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>
<br>
<asp:Image id="Image1"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
runat="server"/>
</AlternatingItemTemplate>
</asp:DataList>
</form>
</body>
</HTML>
至于DataList的分页,网上的例子很多,不再展示,自己去Google一下吧.
文章来源:http://www.blogjava.net/TtTiCk/archive/2006/02/01/29445.html