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; namespace DataListNesting { /// <summary> /// DataListNews 的摘要说明。 /// </summary> public class DataListNews : System.Web.UI.Page { 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; protected System.Web.UI.WebControls.DataList dlParent; protected System.Web.UI.WebControls.LinkButton FirstPage; protected System.Web.UI.WebControls.LinkButton PreviousPage; protected System.Web.UI.WebControls.LinkButton NextPage; protected System.Web.UI.WebControls.LinkButton LastPage; private Int32 CurrentPageNumber=1; private int PageSize=5; private string strConn="server=localhost;uid=sa;pwd=;database=AspNetTest"; 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.dlParent.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.dlParent_ItemDataBound); this.Load += new System.EventHandler(this.Page_Load); } #endregion private DataSet BindParent(int PageSize,int CurrentPageNumber) { SqlConnection Conn=new SqlConnection(strConn); SqlCommand Cmd=new SqlCommand(); Cmd.Connection=Conn; Cmd.CommandType=CommandType.StoredProcedure; Cmd.CommandText="BindNews"; SqlParameter pramsPageSize=new SqlParameter(); pramsPageSize.ParameterName="@PageSize"; pramsPageSize.Value=PageSize; SqlParameter pramsCurrent=new SqlParameter(); pramsCurrent.ParameterName="@PageIndex"; pramsCurrent.Value=CurrentPageNumber; SqlParameter pramsDocount=new SqlParameter(); pramsDocount.ParameterName="@Docount"; pramsDocount.Value=false; Cmd.Parameters.Add(pramsCurrent); Cmd.Parameters.Add(pramsDocount); Cmd.Parameters.Add(pramsPageSize); SqlDataAdapter da=new SqlDataAdapter(); da.SelectCommand=Cmd; DataSet ds=new DataSet(); Conn.Open(); da.Fill(ds); Conn.Close(); ViewState["Count"]=ds.Tables[0].Rows.Count.ToString(); return ds; } private void dlParent_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) { if(e.Item.ItemIndex!=-1) { int ClassID=int.Parse(dlParent.DataKeys[e.Item.ItemIndex].ToString()); DataSet ds=BindChild(ClassID); Label lb=(Label)e.Item.FindControl("lbCount"); lb.Text=" (" + ds.Tables[0].Rows.Count.ToString() + ") "; DataList list=(DataList)e.Item.FindControl("dlChild"); list.DataSource=ds.Tables[0].DefaultView; list.DataBind(); HtmlAnchor hr=(HtmlAnchor)e.Item.FindControl("aurl"); HtmlGenericControl dv=(HtmlGenericControl)e.Item.FindControl("KBChild"); hr.ID="KB"; string ss="expandIt('dlParent__ctl"+e.Item.ItemIndex+"_"+hr.ID+"')"; hr.Attributes.Add("onclick",ss.ToString()); dv.ID=hr.ID+"Child"; string scripts="<script>numTotal=" + int.Parse(ViewState["Count"].ToString()) +";"; for(int i=0;i<int.Parse(ViewState["Count"].ToString());i++) { scripts +="scores[" + i + "]='dlParent__ctl" + i + "_" + hr.ID + "';"; } scripts +="</script>"; Page.RegisterStartupScript("scripts",scripts.ToString()); } } private DataSet BindChild(int ClassID) { SqlConnection Conn=new SqlConnection(strConn); SqlCommand Cmd=new SqlCommand("select * from NewsInfo where ClassID="+ClassID+"",Conn); SqlDataAdapter da=new SqlDataAdapter(); da.SelectCommand=Cmd; DataSet ds=new DataSet(); Conn.Open(); da.Fill(ds); Conn.Close(); return ds; } protected void NavigationLink_Click ( Object sender, CommandEventArgs e )//此事件为HTML代码里注册(事件为OnCommand) { switch ( e.CommandName )//根据设置的CommandName属性判断 { case "First": CurrentPageNumber = 1; break; case "Last": CurrentPageNumber = Int32.Parse ( lbTotalPage.Text ); break; case "Next": CurrentPageNumber = Int32.Parse ( lbCurrentPage.Text ) + 1; break; case "Prev": CurrentPageNumber = Int32.Parse ( lbCurrentPage.Text ) - 1; break; } BindData(); }
public Int32 GetPCount() { SqlConnection Conn=new SqlConnection(strConn); SqlCommand Cmd=new SqlCommand(); Cmd.Connection=Conn; Cmd.CommandType=CommandType.StoredProcedure; Cmd.CommandText="BindNews"; SqlParameter pramsPageSize=new SqlParameter(); pramsPageSize.ParameterName="@PageSize"; pramsPageSize.Value=1; SqlParameter pramsCurrent=new SqlParameter(); pramsCurrent.ParameterName="@PageIndex"; pramsCurrent.Value=1; SqlParameter pramsDocount=new SqlParameter(); pramsDocount.ParameterName="@Docount"; pramsDocount.Value=false; Cmd.Parameters.Add(pramsCurrent); Cmd.Parameters.Add(pramsDocount); Cmd.Parameters.Add(pramsPageSize); Conn.Open(); Int32 Tcount=Int32.Parse(Cmd.ExecuteScalar().ToString()); Conn.Close(); return Tcount; } public void BindData() { lbCurrentPage.Text = CurrentPageNumber.ToString(); //绑定DATAGRID DataSet ds=BindParent(PageSize,CurrentPageNumber); dlParent.DataSource=ds.Tables[0].DefaultView; dlParent.DataBind(); Double TotalPages = 1;//初始化,总页数为1 Double ModePages=0;//取模余数(判断总页数是否要加1) Int32 TotalRecords = GetPCount();//调用上面的得到总记录数的方法 TotalPages = TotalRecords / PageSize;//得到总页数(总记录数除以每页的记录数) 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();//显示页面上的总记录数 //以下为判断点击的按钮(第一页,上一页,下一页,末一页)是否可以用 if ( CurrentPageNumber == 1 ) { PreviousPage.Enabled = false; FirstPage.Enabled=false; if ( TotalPages > 1 ) { NextPage.Enabled = true; LastPage.Enabled=true; } else { NextPage.Enabled = false; LastPage.Enabled=false; } } else { PreviousPage.Enabled = true; FirstPage.Enabled=true; if ( CurrentPageNumber == TotalPages ) { NextPage.Enabled = false; LastPage.Enabled=false; } else { NextPage.Enabled = true; LastPage.Enabled=true; } } ddlPage.Items.Clear();//清楚跳转的页数(如果不清除,里面的记录将会循环增加) int PCount=int.Parse(lbTotalPage.Text);//得到总页数,为了循环 if(PCount==0) { PCount=1; } for(int i=1;i<=PCount;i++) { ddlPage.Items.Add(i.ToString()); } ddlPage.Items.FindByText(CurrentPageNumber.ToString()).Selected=true;//把当前页显示在列表框的第一个 } private void ddlPage_SelectedIndexChanged(object sender, System.EventArgs e) { int PageSize=int.Parse(ddlPage.SelectedValue.ToString()); int PageCount=Int32.Parse(lbTotalPage.Text.Trim().ToString()); if(PageSize<1) { CurrentPageNumber=1; } else if(PageSize>PageCount) { CurrentPageNumber=PageCount; } else { CurrentPageNumber=PageSize; } BindData(); } } }
|