using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using ClassDataBase; namespace WLNet.YellowPage { /// <summary> /// ShowYellowPage 的摘要说明。 /// </summary> public class ShowYellowPage : System.Web.UI.Page { private ClassYPage YPage=new ClassYPage(); protected System.Web.UI.WebControls.Label lbTotalPage; protected System.Web.UI.WebControls.Label lbTotalCount; protected System.Web.UI.WebControls.Label lbCurrentPage; protected System.Web.UI.WebControls.DropDownList ddlPage; private string strConn=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString(); protected Int32 CurrentPageNumber = 1; protected System.Web.UI.WebControls.HyperLink lnkPrev; protected System.Web.UI.WebControls.HyperLink lnkNext; protected System.Web.UI.WebControls.DataList dlParent; protected System.Web.UI.WebControls.HyperLink lnkFirst; protected System.Web.UI.WebControls.HyperLink lnkLast;//初始化,设置当前页为1,随时都要使用 protected int PageSize=10; private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { BindData(); } } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.ddlPage.SelectedIndexChanged += new System.EventHandler(this.ddlPage_SelectedIndexChanged); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void BindData() { SqlConnection Conn=new SqlConnection(strConn); SqlCommand cmd=new SqlCommand("GetIndexPage",Conn); cmd.CommandType=CommandType.StoredProcedure; SqlDataAdapter da=new SqlDataAdapter(); da.SelectCommand=cmd; DataSet ds=new DataSet(); da.Fill(ds,"IndexPage"); SqlCommand cmd1=new SqlCommand("GetSePage",Conn); cmd1.CommandType=CommandType.StoredProcedure; SqlDataAdapter da1=new SqlDataAdapter(); da1.SelectCommand=cmd1; da1.Fill(ds,"SePage"); ds.Relations.Add("MyRelations",ds.Tables["IndexPage"].Columns["IndexID"], ds.Tables["SePage"].Columns["IndexID"]); PagedDataSource Pds = new PagedDataSource(); Pds.DataSource = ds.Tables["IndexPage"].DefaultView; Pds.AllowPaging = true; Pds.PageSize = 10; int CurrentPageNumber; int PageSize=Pds.PageSize; //当前页面从Page查询参数获取 if (Request.QueryString["Page"] != null) CurrentPageNumber=Convert.ToInt32(Request.QueryString["Page"]); else CurrentPageNumber=1; int TotalRecords=ds.Tables[0].Rows.Count; int TotalPages = TotalRecords / PageSize;//得到总页数(总记录数除以每页的记录数) int ModePages=TotalRecords%PageSize;//得到取模的余数(总记录数取模每页的记录数) if(ModePages>0)//如果取模数不等于0,则把总页数加1 { TotalPages+=1; } if(ModePages==0)//如果取模数等于0,不做任何事(也可以不用判断) { } if(TotalPages==0) { TotalPages=1; } lbTotalPage.Text = TotalPages.ToString();//显示页面上的总页数 lbTotalCount.Text=TotalRecords.ToString();//显示页面上的总记录数 Pds.CurrentPageIndex = CurrentPageNumber-1; lbCurrentPage.Text=CurrentPageNumber.ToString(); if ( CurrentPageNumber == 1 ) { lnkFirst.Enabled = false; lnkPrev.Enabled=false; if ( TotalPages > 1 ) { lnkNext.Enabled = true; lnkLast.Enabled=true; } else { lnkNext.Enabled = false; lnkLast.Enabled=false; } } else { lnkPrev.Enabled = true; lnkFirst.Enabled=true; if ( CurrentPageNumber == TotalPages ) { lnkNext.Enabled = false; lnkLast.Enabled=false; } else { lnkNext.Enabled = true; lnkLast.Enabled=true; } } if (!Pds.IsFirstPage) { lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + " ?Page=" + Convert.ToString(CurrentPageNumber-1); lnkFirst.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + 1; } if (!Pds.IsLastPage) { lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurrentPageNumber+1); lnkLast.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(TotalPages); } ddlPage.Items.Clear();//清楚跳转的页数(如果不清除,里面的记录将会循环增加) int PCount=int.Parse(lbTotalPage.Text);//得到总页数,为了循环 for(int i=1;i<=PCount;i++) { ddlPage.Items.Add(i.ToString()); } ddlPage.Items.FindByText(CurrentPageNumber.ToString()).Selected=true;//把当前页显示在列表框的第一个 //把PagedDataSource 对象赋给Repeater控件 dlParent.DataSource=Pds; dlParent.DataBind(); } private void ddlPage_SelectedIndexChanged(object sender, System.EventArgs e) { int PageSize=int.Parse(ddlPage.SelectedValue.ToString()); Server.Transfer("ShowYellowPage.aspx?Page="+PageSize); BindData(); } } }
|