使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页

 

     最近做一个关于三级联动的例子。想在页面上显示像树形控件一样的效果,每一个大类下面有各自的子类。所以使用了Relations建立了表之间的关系(Relations是获取用于将表链接起来并允许从父表浏览到子表的关系的集合)
并却使用了PagedDataSource对DataList进行了分页(PagedDataSource封装 DataGrid 控件的属性,这些属性使该控件可以执行分页).DataGrid的自带的分页就是使用的这个类。所以不能进行大量数据的分页.

首先建立两张表,SQL语句如下:
IndexPage为主表,SePage为子表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IndexPage]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[IndexPage]
GO

CREATE TABLE [dbo].[IndexPage] (
 [IndexID] [int] IDENTITY (1, 1) NOT NULL ,
 [IndexName] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
 [Content] [varchar] (1000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SePage]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[SePage]
GO

CREATE TABLE [dbo].[SePage] (
 [SeID] [int] IDENTITY (1, 1) NOT NULL ,
 [IndexID] [int] NULL ,
 [SeName] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
 [Content] [varchar] (1000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

存储过程:(1)GetIndexPage
CREATE PROC GetIndexPage
AS
SELECT * FROM INDEXPAGE

(2)GetSecondPage
CREATE PROC GetSecondPage
AS
SELECT * FROM SEPAGE

基本效果
HTML代码:
 
<%@ Page language="c#" Codebehind="ShowYellowPage.aspx.cs" AutoEventWireup="false" Inherits="WLNet.YellowPage.ShowYellowPage" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>ShowYellowPage</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <LINK href="../CSS/BasicLayout.css" type="text/css" rel="stylesheet">
  <meta content="
http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <table class="GbText" style="BORDER-COLLAPSE: collapse" borderColor="#cccccc" width="100%"
    align="center" border="1">
    <TR>
     <TD style="HEIGHT: 15px" align="center"><font style="FONT-SIZE: 10pt">共</font>
      <asp:label id="lbTotalPage" runat="server"></asp:label><font style="FONT-SIZE: 10pt">页/共
       <asp:label id="lbTotalCount" runat="server"></asp:label>记录&nbsp; 当前</font>
      <asp:label id="lbCurrentPage" runat="server"></asp:label><font style="FONT-SIZE: 10pt">页</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <font style="FONT-SIZE: 10pt">跳转到</font>
      <asp:dropdownlist id="ddlPage" runat="server" Width="65px" AutoPostBack="True"></asp:dropdownlist><font style="FONT-SIZE: 10pt">页</font></TD>
    </TR>
    <tr>
     <td><asp:datalist id="dlParent" runat="server" Width="100%" RepeatColumns="2">
       <ItemStyle Font-Size="X-Small"></ItemStyle>
       <ItemTemplate>
        <A href='<%# "ShowData.aspx?IndexID="+DataBinder.Eval(Container,"DataItem.IndexID")%>'>
         <%# DataBinder.Eval(Container,"DataItem.IndexName")%>
        </A>
        <asp:DataList id="dlChild" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("MyRelations") %>'>
         <ItemTemplate>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='<%# "ShowData.aspx?SeID="+DataBinder.Eval(Container.DataItem, "[/"SeID/"]")%>'><%# DataBinder.Eval(Container.DataItem, "[/"SeName/"]")%></a>
          <br>
         </ItemTemplate>
        </asp:DataList>
       </ItemTemplate>
       <HeaderStyle Font-Size="X-Small"></HeaderStyle>
      </asp:datalist></td>
    </tr>
    <tr>
     <TD align="center" height="30"><b><asp:hyperlink id="lnkFirst" runat="server">第一页</asp:hyperlink>&nbsp;&nbsp;&nbsp;&nbsp;<asp:hyperlink id="lnkPrev" runat="server">上一页</asp:hyperlink>&nbsp;&nbsp;
       <asp:hyperlink id="lnkNext" runat="server">下一页</asp:hyperlink>&nbsp;&nbsp;&nbsp;
       <asp:HyperLink id="lnkLast" runat="server">末一页</asp:HyperLink></b></TD>
    </tr>
   </table>
  </form>
 </body>
</HTML>

CS代码:

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();
  }

 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值