前台:
<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("⊙&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"Newscontent.aspx?ID=" + n.ID + "\">" + n.Title + "</a>");
                       //sb.Append("∷&nbsp;<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;
    }
    
}出现的问题是每次读出的数据能出现两次,求高手指点怎么改,可以解决这个问题谢谢了

解决方案 »

  1.   

    再以后发代码的时候,最好用CSDN文本编辑器里自带的代码工具发,
    看起来方便。期待高手指点,学习了。
      

  2.   

    AspNetPager1_PageChanged又调用了一次。
      

  3.   

    因为你用的是url分页,页面每次加载都会引发分页事件,所以就不需要在page_load中再调用这段代码:
    if (!Page.IsPostBack)
      {
      BindGridView();
      }
    aspnetpager示例中有详细说明,请参考一下
      

  4.   

    膜拜一下大神,8次MICROSOFT的MVP
      

  5.   

    膜拜一下大神,8次MICROSOFT的MVP
      

  6.   

    开启了URL分页,页面加载就会执行PageChanged事件