前台:
<div id="Pbox">
<%
StringBuilder sb = new StringBuilder();
foreach (Model.Pro n in prolist)
{
sb.Append("<div class=\"Pimg\">");
sb.Append("<a href="+n.dizhi+" rel=\"lightbox[roadtrip]\" title="+n.Title+">");
sb.Append("<img class=\"pro_img\" src=" + n.dizhi + " title=" + n.Title + " /></a></div>");
////sb.Append("⊙ <a href=\"Newscontent.aspx?ID=" + n.ID + "\">" + n.Title + "</a>");
//sb.Append("∷ <a href=\"Newscontent.aspx?ID=" + n.ID + "\">" + n.Title + "</a>");
//sb.Append("</div>");
} Response.Write(sb.ToString());
%>
</div> <div class="page">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" UrlPaging="true"
PageSize="1" ShowCustomInfoSection="Left"
NumericButtonTextFormatString="[{0}]"
ShowBoxThreshold="5"
AlwaysShow="true"
OnPageChanged="AspNetPager1_PageChanged" >
</webdiyer:AspNetPager>
</div>后台:
using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class Product : System.Web.UI.Page
{ public List<Model.Pro> prolist = new List<Model.Pro>();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView();
} } protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGridView();
} public void BindGridView()
{
int type = Convert.ToInt32(Request["type"]);
string sql = "SELECT ID,imgname,imgcontents,dizhi FROM [product_contents] WHERE typeid =" + type;//自定义的SQL语句 int recordcount;
DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount);
this.AspNetPager1.RecordCount = recordcount;
Model.Pro proModel;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
proModel = new Model.Pro();
proModel.ID = (int)ds.Tables[0].Rows[i]["ID"];
proModel.Title = ds.Tables[0].Rows[i]["imgname"].ToString();
proModel.Contents = ds.Tables[0].Rows[i]["imgcontents"].ToString();
proModel.dizhi = ds.Tables[0].Rows[i]["dizhi"].ToString(); prolist.Add(proModel);
} AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
} public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
{
string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strConnnection += Server.MapPath("~/App_Data/DataMaster.mdb");
OleDbConnection coon = new OleDbConnection(strConnnection); OleDbDataAdapter ada = new OleDbDataAdapter(sql,coon); DataSet ds = new DataSet();
int startRow = (currentPage - 1) * pagesize;
ada.Fill(ds, startRow, pagesize, "table");
recordcount = GetPageRecord(sql);
return ds;
} public int GetPageRecord(string sql)
{
string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strConnnection += Server.MapPath("~/App_Data/DataMaster.mdb");
OleDbConnection coon = new OleDbConnection(strConnnection); sql = System.Text.RegularExpressions.Regex.Replace(sql, "ORDER BY.*", "");
sql = "select count(*) from (" + sql + ") as temp";
OleDbCommand cmd = new OleDbCommand(sql,coon);
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
return recordcount;
}
}出现的问题是每次读出的数据能出现两次,求高手指点怎么改,可以解决这个问题谢谢了
<div id="Pbox">
<%
StringBuilder sb = new StringBuilder();
foreach (Model.Pro n in prolist)
{
sb.Append("<div class=\"Pimg\">");
sb.Append("<a href="+n.dizhi+" rel=\"lightbox[roadtrip]\" title="+n.Title+">");
sb.Append("<img class=\"pro_img\" src=" + n.dizhi + " title=" + n.Title + " /></a></div>");
////sb.Append("⊙ <a href=\"Newscontent.aspx?ID=" + n.ID + "\">" + n.Title + "</a>");
//sb.Append("∷ <a href=\"Newscontent.aspx?ID=" + n.ID + "\">" + n.Title + "</a>");
//sb.Append("</div>");
} Response.Write(sb.ToString());
%>
</div> <div class="page">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" UrlPaging="true"
PageSize="1" ShowCustomInfoSection="Left"
NumericButtonTextFormatString="[{0}]"
ShowBoxThreshold="5"
AlwaysShow="true"
OnPageChanged="AspNetPager1_PageChanged" >
</webdiyer:AspNetPager>
</div>后台:
using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class Product : System.Web.UI.Page
{ public List<Model.Pro> prolist = new List<Model.Pro>();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView();
} } protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGridView();
} public void BindGridView()
{
int type = Convert.ToInt32(Request["type"]);
string sql = "SELECT ID,imgname,imgcontents,dizhi FROM [product_contents] WHERE typeid =" + type;//自定义的SQL语句 int recordcount;
DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount);
this.AspNetPager1.RecordCount = recordcount;
Model.Pro proModel;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
proModel = new Model.Pro();
proModel.ID = (int)ds.Tables[0].Rows[i]["ID"];
proModel.Title = ds.Tables[0].Rows[i]["imgname"].ToString();
proModel.Contents = ds.Tables[0].Rows[i]["imgcontents"].ToString();
proModel.dizhi = ds.Tables[0].Rows[i]["dizhi"].ToString(); prolist.Add(proModel);
} AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
} public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
{
string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strConnnection += Server.MapPath("~/App_Data/DataMaster.mdb");
OleDbConnection coon = new OleDbConnection(strConnnection); OleDbDataAdapter ada = new OleDbDataAdapter(sql,coon); DataSet ds = new DataSet();
int startRow = (currentPage - 1) * pagesize;
ada.Fill(ds, startRow, pagesize, "table");
recordcount = GetPageRecord(sql);
return ds;
} public int GetPageRecord(string sql)
{
string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strConnnection += Server.MapPath("~/App_Data/DataMaster.mdb");
OleDbConnection coon = new OleDbConnection(strConnnection); sql = System.Text.RegularExpressions.Regex.Replace(sql, "ORDER BY.*", "");
sql = "select count(*) from (" + sql + ") as temp";
OleDbCommand cmd = new OleDbCommand(sql,coon);
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
return recordcount;
}
}出现的问题是每次读出的数据能出现两次,求高手指点怎么改,可以解决这个问题谢谢了
看起来方便。期待高手指点,学习了。
if (!Page.IsPostBack)
{
BindGridView();
}
aspnetpager示例中有详细说明,请参考一下