aspx页面Repeater嵌套Repeater

//--------------以下为*.aspx页面Repeater嵌套Repeater:----------------------------------------

<table width="700" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="21" valign="top" background="http://images.cnblogs.com/pic/small-Tbl-left-bg.jpg"><img src="http://images.cnblogs.com/pic/small-Tbl_02.jpg" width="21" height="62" alt=""></td>
                    <td valign="top" class="jianbian-B"><table style="margin-top:5px;" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="20"><img src="http://images.cnblogs.com/pic/tubiao-1.jpg" /></td>
                        <td class="STYLE-chenghuang-font"> *****</td>
                      </tr>
                    </table>
     <table style="margin-top:20px" width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="40"><span style="FLOAT: left">届数:</span></td>
                        <td width="70"><asp:TextBox ID="txtFallNum" runat="server" Width="50px"></asp:TextBox></td>
                        <td width="40"><div align="center">日期:</div></td>
                        <td width="70">
          <asp:dropdownlist id="ddlStartDate" runat="server" Width="80px">
                                 <asp:ListItem Value="">不限</asp:ListItem>
       <asp:ListItem Value="09-07">9月7日</asp:ListItem>
       <asp:ListItem Value="09-08">9月8日</asp:ListItem>
       <asp:ListItem Value="09-09">9月9日</asp:ListItem>
       <asp:ListItem Value="09-10">9月10日</asp:ListItem>
       <asp:ListItem Value="09-11">9月11日</asp:ListItem>
       <asp:ListItem Value="09-12">9月12日</asp:ListItem>
                          </asp:dropdownlist></td>
                        <td width="40"><div align="center">至</div></td>
                        <td> 
           <asp:dropdownlist id="ddlEndDate" runat="server" Width="80px">
                                <asp:ListItem Value="">不限</asp:ListItem>
       <asp:ListItem Value="09-07">9月7日</asp:ListItem>
       <asp:ListItem Value="09-08">9月8日</asp:ListItem>
       <asp:ListItem Value="09-09">9月9日</asp:ListItem>
       <asp:ListItem Value="09-10">9月10日</asp:ListItem>
       <asp:ListItem Value="09-11">9月11日</asp:ListItem>
       <asp:ListItem Value="09-12">9月12日</asp:ListItem>
                          </asp:dropdownlist>名称:<asp:textbox id="txtMeeting" runat="server" Width="130px"></asp:textbox>
                                <asp:button id="btnFind" runat="server" CssClass="btn_2k3" Text="查询" OnClick="btnFind_Click"></asp:button>
                              <asp:regularexpressionvalidator id="re" Runat="server" ValidationExpression="\d+" ControlToValidate="txtFallnum"
           ErrorMessage="请输入整数!"></asp:regularexpressionvalidator></td>
                      </tr>
                    </table>
     <table style="margin-top:10px;" id="Table2" cellspacing="0" cellpadding="0" width="100%" border="0" runat="server">
                <tr>
                  <td>
                  <asp:Repeater id="rptCategories" runat="server" OnItemDataBound="rptCategories_ItemDataBound">
                      <HeaderTemplate>
                        <table width="100%" border="1" bordercolor="#FFB24D"cellspacing="0" cellpadding="0" style="border-collapse:collapse; line-height:1.5em">
                        <tr style="text-align: center;" >
                        <td class="chengsetitle">
                           会议名称
                        </td>
                        <td class="chengsetitle">
                            日期
                        </td>
                        <td class="chengsetitle">
                           时间
                        </td>
                        <td class="chengsetitle">
                           地点
                        </td>
                        <td class="chengsetitle">
                           在线报名
                        </td>
                        </tr>
                      </HeaderTemplate>
                      <ItemTemplate>
                        <!--分类名称-->
                        <tr style="text-align: left;height:30px"><td colspan="5" ><b><%# Eval("ClassName") %></b></td></tr>
                        <!--分类下的明细-->
                        <asp:Repeater id="rptProduct" runat="server">
                          <ItemTemplate>
                            <tr style="text-align: left;height:26px">
            <td style="text-align: left;180px">
                <asp:HyperLink id="lbtnMeeting" runat="server" NavigateUrl='<%# DataBinder.Eval(Container, "DataItem.f_id")+".html"%>' Target="_blank" Text='<%# DataBinder.Eval(Container, "DataItem.Meeting") %>'> </asp:HyperLink>
            </td>
            <td style="text-align: center;60px" ><%# Eval("StartDate")%>
            </td>
            <td style="text-align: center;100px">
                <%# Eval("endDate")%>
            </td>
            <td style="text-align: left;150px">
                <%# Eval("adress")%>
            </td>
            <td style="text-align: center;50px">
               <a href='<%# "*****.aspx?f_id="+DataBinder.Eval(Container, "DataItem.F_ID") %>' target="_blank"><%# DataBinder.Eval(Container, "DataItem.IsSign").ToString() == "1"?"报名":""%></a>
            </td>
                            </tr>
                          </ItemTemplate>
                        </asp:Repeater>
                      </ItemTemplate>
                      <FooterTemplate>
                        </table>
                      </FooterTemplate>
                    </asp:Repeater>
             
                  </td>
                </tr>
                <tr style="height:35px">
                  <td align="right">
                      <uc2:Pager ID="Pager1" runat="server" />
                  </td>
                </tr>
            </table>
     </td>
                    <td ></td>
                  </tr>
                </table>

//-----------------------以下为页面代码-------------------------------

 

        Security mySecurity;
        int rptRowsCount;//初始化嵌套rpt的当前已显示行的行数
        string rptClassName;//rpt前一页最后一个ClassName
        int rptClassNameCount;//rpt前一页最后一个ClassName显示在rpt上的行数

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string strFallnum = txtFallNum.Text.Trim();
                string strWhere = "";
                if (Request["list"] != null)
                {
                    #region 页面附值
                    mySecurity = new Security();
                    string pars = mySecurity.DecryptQueryString(Request["list"]);//解密
                    string[] tmplist = pars.Split(',');

                    this.txtFallNum.Text = tmplist[1];
                    this.ddlStartDate.SelectedIndex = this.ddlStartDate.Items.IndexOf(this.ddlStartDate.Items.FindByValue(tmplist[2]));
                    this.ddlEndDate.SelectedIndex = this.ddlEndDate.Items.IndexOf(this.ddlEndDate.Items.FindByValue(tmplist[3]));
                    this.txtMeeting.Text = tmplist[4];
                    if (ViewState["strWhere"] != null)
                    {
                        strWhere = ViewState["strWhere"].ToString();
                    }
                    else
                    {
                        strWhere = this.getWhere();
                    }
                    #endregion
                }
                else
                {
                    #region 页面初始
                    this.txtFallNum.Text = SysConfig.GetFallNum();
                    strWhere = this.getWhere();
                    #endregion
                }
                DataBind(strWhere);
            }
        }

        #region 绑定Repeater
        private void DataBind(string strCondition)
        {
            rptRowsCount = 0;//初始化嵌套rpt的当前已显示行的行数为0

            #region 条件
            string fldName = "DictID,F_id,ClassName,Meeting,Sponsor,UnderTaker,Coopertion,Subject,"
                + " endDate,Adress,StartDate,Scale,Speaker,Content,SysDate,ContactWay,OrderBy,"
                + "issign ,newOrderBy=(case when OrderBy is null then 1000 else OrderBy end)";
            string tblName = "T_Forum_Info t left join t_sysdictdetail s on t.ClassName=s.DictName_C";
            strCondition += " and beforecheck=1 and lang=0 and tablename='t_forum_info' and fieldname='classname'";// Order by newOrderBy";
            #endregion

            #region 绑定分页
            Pager1.UrlFormat = "?list=";//分页格式
            Pager1.RecordCount = dbAccess.GetCount(tblName, strCondition, fldName);//取得总记录数
            Pager1.PageSize = SysConfig.GetPageSize("Forum");//为0时,默认读取配置文件的分页数
            #endregion

            #region 绑定数据
            int pageIndex;
            if (this.Pager1.SetPageIndex == 1)
                pageIndex = 1;
            else
                pageIndex = this.Pager1.PageIndex;
            DataSet ds,ds_cn;
            int StartNum = this.Pager1.PageSize * (this.Pager1.PageIndex - 1) + 1;
            int EndNum = this.Pager1.PageSize * this.Pager1.PageIndex;
            string tblName_P = "(" + getPageList(tblName, fldName, StartNum, EndNum, "DictID,StartDate,endDate", true, strCondition, "F_ID") + ") as a";
            ds = dbAccess.GetDataSet(tblName_P, "distinct DictID,ClassName", "", "DictID");
            this.Pager1.PageCount = System.Convert.ToInt32(Math.Ceiling((Decimal)Pager1.RecordCount / Pager1.PageSize));//获取页数

            #region rptClassName和rptClassNameCount值计算
            if (this.Pager1.PageIndex == 1)
            {
                //当前页为第一页的时候
                rptClassName = "";
                rptClassNameCount = 0;
                if(ds.Tables[0].Rows.Count>0)
                    rptClassName = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["CLassName"].ToString();//放在这个位置则取rpt的最后一个类别
            }
            else
            {
                //取得前一页的最后一个列名和列的显示数
                StartNum = this.Pager1.PageSize * (pageIndex - 1 - 1) + 1;
                EndNum = this.Pager1.PageSize * (pageIndex - 1);
                tblName_P = "(" + getPageList(tblName, fldName, StartNum, EndNum, "DictID,StartDate,endDate", true, strCondition, "F_ID") + ") as a";
                ds_cn = dbAccess.GetDataSet(tblName_P, "", "", "DictID");
                int ss = 0;
                //获取前页的最后一个类别,为了判断下一页与本页最后一个类别做判断
                if (ds_cn.Tables[0].Rows.Count > 0)
                {
                    rptClassName = ds_cn.Tables[0].Rows[ds_cn.Tables[0].Rows.Count - 1]["CLassName"].ToString();
                    for (int i = 0; i < ds_cn.Tables[0].Rows.Count; i++)
                    {
                        if (ds_cn.Tables[0].Rows[i]["CLassName"].ToString() == rptClassName)
                            ss++;
                    }
                    //如果前一页的最后一个列名的列的显示数>=每月显示数,且当前页>2,那么继续判断

                    //这里只判断了翻2页的情况,如果类别大于3页那么要写个循环判断
                    if (ss >= this.Pager1.PageSize && this.Pager1.PageIndex > 2)
                    {
                        tblName_P = "(" + getPageList(tblName, fldName, this.Pager1.PageSize, pageIndex - 2, "DictID,StartDate,endDate", true, strCondition, "F_ID") + ") as a";
                        ds_cn = dbAccess.GetDataSet(tblName_P, "", "", "DictID");
                        for (int i = 0; i < ds_cn.Tables[0].Rows.Count; i++)
                        {
                            if (ds_cn.Tables[0].Rows[i]["CLassName"].ToString() == rptClassName)
                                ss++;
                        }
                    }
                    rptClassNameCount = ss;
                }
                else
                {
                    rptClassName = "";
                    rptClassNameCount = 0;
                }
            }
            #endregion

            Pager1.UrlPar = "{0}," + this.txtFallNum.Text.Trim() + "," + this.ddlStartDate.SelectedValue + "," + this.ddlEndDate.SelectedValue + "," + this.txtMeeting.Text.Trim();//跳转页面 查询条件的值
           
            this.rptCategories.DataSource = ds;
            this.rptCategories.DataBind();
            #endregion
        }
        #endregion

        protected void btnFind_Click(object sender, EventArgs e)
        {
            string strWhere = getWhere();
            ViewState["strWhere"] = strWhere;
            this.Pager1.SetPageIndex = 1;//每次查询初始化当前页为第一页
            DataBind(strWhere);
        }

        private string getWhere()
        {
            string strStartDate = ddlStartDate.SelectedItem.Value;
            string strEndDate = ddlEndDate.SelectedItem.Value;
            string strMeeting = txtMeeting.Text.Trim();
            string strFallnum = txtFallNum.Text.Trim();
            string strWhere = "";
            if (strStartDate != "")
            {
                strWhere += " and CONVERT(DATETIME,CONVERT(VARCHAR,YEAR(GETDATE()))+'-'+STARTDATE) >= CONVERT(VARCHAR,YEAR(GETDATE()))+'-'+'" + strStartDate + "'";
            }
            if (strEndDate != "")
            {
                strWhere += " and CONVERT(DATETIME,CONVERT(VARCHAR,YEAR(GETDATE()))+'-'+STARTDATE) <= CONVERT(VARCHAR,YEAR(GETDATE()))+'-'+'" + strEndDate + "'";
            }

            if (strFallnum != string.Empty)
            {
                strWhere += " and fallnum =" + strFallnum;
            }
            if (strMeeting != string.Empty)
            {
                strWhere += " and meeting like '%" + strMeeting + "%'";
            }
            return strWhere;
        }

        protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //初始化嵌套Repeater的当前已显示行的行数小于每页显示数的情况下
                int flag = 0;
                if (this.Pager1.PageIndex == this.Pager1.PageCount)
                {
                    //当前页=最后一页的情况,计算最后一页的记录数//否则默认
                    flag = Pager1.RecordCount - (this.Pager1.PageIndex - 1) * this.Pager1.PageSize;
                }
                else
                {                   
                    flag = this.Pager1.PageSize;
                }
                if (rptRowsCount < flag)
                {
                    Repeater rptProduct = (Repeater)e.Item.FindControl("rptProduct");//查找嵌套的Repeater
                    DataRowView rowv = (DataRowView)e.Item.DataItem; //找到分类Repeater关联的数据项
                    string className = rowv["ClassName"].ToString();//提取分类名称
                    DataBindRpt(rptProduct, className);//根据分类名称查询该分类对应的数据,并绑定到Repeater
                }
            }
        }

        #region 绑定嵌套的Repeater
        private void DataBindRpt(Repeater rptProduct, string className)
        {
            #region 条件
            string fldName = "DictID,F_id,ClassName,Meeting,Sponsor,UnderTaker,Coopertion,Subject,"
                + " endDate,Adress,(SubString(StartDate,1,2)+'月'+SubString(StartDate,4,5)+'日') as StartDate,Scale,Speaker,Content,SysDate,ContactWay,OrderBy,"
                + "issign ,newOrderBy=(case when OrderBy is null then 1000 else OrderBy end)";
            string tblName = "T_Forum_Info t left join t_sysdictdetail s on t.ClassName=s.DictName_C";
            string strCondition = getWhere();
            strCondition += " and beforecheck=1 and lang=0 and tablename='t_forum_info' and fieldname='classname'";// Order by newOrderBy";
            strCondition += " and ClassName='" + className + "'";
            string strOrder = "OrderBy";
            #endregion

            #region 绑定数据
            if (rptRowsCount < this.Pager1.PageSize)//当Repeater的记录数小于当前页的记录数的时
            {
                int size = 0;//初始化 剩余的查询记录数为0
                if (this.Pager1.PageIndex == this.Pager1.PageCount)//当前页=最后一页的情况
                {
                    size = Pager1.RecordCount - (this.Pager1.PageIndex - 1) * this.Pager1.PageSize - rptRowsCount;//剩余的查询记录数 = 最后一页显示记录数 - 已查询记录数
                }
                else
                {
                    size = this.Pager1.PageSize - rptRowsCount;//剩余的查询记录数 = 当前页总记录数 - 已查询记录数
                }
                DataSet ds;
                int pageIndex = 1;
                int StartNum;//起始记录数
                int EndNum;//结尾记录数
                if (className == rptClassName)//类别相同,过滤已经查询的记录
                {
                    //假设50条/页--已经查询2条,那么从第3条开始查询
                    StartNum = rptClassNameCount + 1;
                    EndNum = StartNum + size;
                    string tblName_P = "(" + getPageList(tblName, fldName, StartNum, EndNum, strOrder, true, strCondition, "F_ID") + ") as a";
                    ds = dbAccess.GetDataSet(tblName_P, "", "", strOrder);
                    rptRowsCount = rptRowsCount + ds.Tables[0].Rows.Count;
                }
                else//列别不一样
                {
                    StartNum = 1;
                    EndNum =  size;
                    string tblName_P = "(" + getPageList(tblName, fldName, StartNum, EndNum, strOrder, true, strCondition, "F_ID") + ") as a";
                    ds = dbAccess.GetDataSet(tblName_P, "", "", strOrder);
                    rptRowsCount = rptRowsCount + ds.Tables[0].Rows.Count;
                }
                rptProduct.DataSource = ds;
                rptProduct.DataBind();
            }
            #endregion
        }
        #endregion

        #region 改写dbAccess.getPageList的分页SQL,具体使用详见dbAccess.getPageList
        public string getPageList(string tblName, string fldName, int StartNum, int EndNum,
string fldSort, bool Sort, string strCondition, string ID)
        {
            if (string.IsNullOrEmpty(fldName))
                fldName = "*";
            if (string.IsNullOrEmpty(fldSort))
                fldSort = ID;
            if (Sort)
                fldSort = fldSort + " desc ";
            StringBuilder strSql = new StringBuilder("select * from (select ROW_NUMBER() OVER(ORDER BY " + fldSort + ")  as rowNum," + fldName + " from " + tblName + " where 1=1 " + strCondition + ") as a where rowNum between " + StartNum + " and " + EndNum);
            return strSql.ToString();
        }
        #endregion
    }

//---------------------通用分页方法出自于廖永军的代码生成器---------------------------------------------

        /// <summary>
        /// 通用分页方法
        /// </summary>
        /// <param name="tblName">要显示的表或多个表的连接</param>
        /// <param name="fldName">要显示的字段列表,可为Null,表示*</param>
        /// <param name="pageSize">每页显示的记录个数</param>
        /// <param name="pageIndex">要显示那一页的记录</param>
        /// <param name="fldSort">排序字段列表或条件</param>
        /// <param name="Sort">排序方法,False为升序,True为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')</param>
        /// <param name="strCondition">查询条件,不需where,以And开始,可为Null,表示""</param>
        /// <param name="ID">主表的主键</param>
        /// <returns>查询当前页的数据集</returns>
        public static DataSet PageList(string tblName, string fldName, int pageSize, int pageIndex,
            string fldSort, bool Sort, string strCondition, string ID)
        {
            if (string.IsNullOrEmpty(fldName))
                fldName = "*";
            if (pageSize == 0)
                pageSize = int.Parse(ConfigurationSettings.AppSettings["PageSize"]);
            if (string.IsNullOrEmpty(fldSort))
                fldSort = ID;
            if (Sort)
                fldSort = fldSort + " desc ";
            StringBuilder strSql = new StringBuilder("select * from (select ROW_NUMBER() OVER(ORDER BY " + fldSort + ")  as rowNum," + fldName + " from " + tblName + " where 1=1 " + strCondition + ") as a where rowNum between " + (pageSize * (pageIndex - 1) + 1) + " and " + pageSize * pageIndex);
            return ExecuteQuery(strSql.ToString());
        }

        /// <summary>
        /// 得到数据总条数,在单表统计时使用
        /// </summary>
        /// <param name="strCondition">条件</param>
        public static int GetCount(string tblName, string strCondition)
        {
            DataSet ds = GetDataSet(tblName, "count(*)", strCondition, null);
            return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
        }

        /// <summary>
        /// 得到数据总条数,在多表统计时使用
        /// </summary>
        /// <param name="strCondition">条件</param>
        public static int GetCount(string tblName, string strCondition, string fldName)
        {
            if (string.IsNullOrEmpty(fldName))
                fldName = "*";
            DataSet ds = GetDataSet(tblName, fldName, strCondition, null);
            return Convert.ToInt32(ds.Tables[0].Rows.Count);
        }

原文地址:https://www.cnblogs.com/yongheng178/p/1274299.html