Repeater嵌套

前台 

<table width="600" height="25" border="0" align="center" cellpadding="0" cellspacing="0">
 <asp:Repeater id="rpt" runat="server">
  <ItemTemplate>
   <tr>
    <td width="41" align="center">&gt;</td>
    <td width="98" align="center"><%# DataBinder.Eval(Container.DataItem,"powername") %></td>
    <td width="414" align="left">
     <table width="85" height="25" border="0" cellpadding="0" cellspacing="0">
      <tr>
       <asp:Repeater id="subrpt" runat="server">
        <ItemTemplate>
         <td align="center"><%# DataBinder.Eval(Container.DataItem,"powername") %></td>
        </ItemTemplate>
       </asp:Repeater></tr>
     </table>
    </td>
    <td width="47" align="center">更多</td>
   </tr>
  </ItemTemplate>
 </asp:Repeater>
</table>


后台
using System;
 using System.Data;
 using System.Drawing;
 using System.Data.SqlClient;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;

 /// <summary>
 ///  ShowAll 的摘要说明。
 /// </summary>
 public class ShowAll : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.Repeater subrpt;
  protected System.Web.UI.WebControls.Repeater rpt;
  public string CategoryID="0";
  public string WebCount="4";

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.IsPostBack)
   {
    SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
    SqlDataAdapter cmd1=new SqlDataAdapter("select powername,id from navigationsort where parentid="+CategoryID+" order by orderid desc",con);
    DataSet ds=new DataSet();
    cmd1.Fill(ds,"pt");
    rpt.DataSource=ds.Tables["pt"];
    rpt.DataBind();
    con.Close();
   }
  }

  private void rpt_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
  {
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    Repeater subrpt = (Repeater) e.Item.FindControl("subrpt");
    DataRowView rowv = (DataRowView)e.Item.DataItem;
    string ParentId = rowv["id"].ToString();
    subrpt.DataSource=getsub(ParentId);
    subrpt.DataBind();
   }
  }
  private DataSet getsub(string id)
  {
   SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
   SqlDataAdapter cmd1=new SqlDataAdapter("select top "+WebCount+" title,id from navigation where sortid="+id,con);
   DataSet ds=new DataSet();
   cmd1.Fill(ds);
   con.Close();
   return ds;
  }
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值