2010-08-29 97 views
1

我的代碼的一部分,我窩中繼器在HTML硬編碼他們:動態嵌套中繼器ASP.net

<asp:Repeater ID="rpt_1" runat="server"> 
     <ItemTemplate> 
      <div><%# Eval("ID") %></div> 
      <asp:Repeater ID="rpt_2" runat="server"> 
       <ItemTemplate> 
         <div><%# Eval("ID") %></div> 
         <asp:Repeater ID="rpt_3" runat="server"> 
          <ItemTemplate> 
           <div><%# Eval("ID") %></div> 
          </ItemTemplate> 
         </asp:Repeater> 
       </ItemTemplate> 
      </asp:Repeater> 
     </ItemTemplate> 
    </asp:Repeater> 

每個轉發器的ItemTemplate具有完全相同的HTML和佈局的東西。有沒有一種方法可以根據一種佈局將它們編入n箇中繼器中?這裏有點什麼,我想要做的pseduo代碼:

 <asp:Repeater ID="rpt_1" runat="server"> 
     <ItemTemplate> 
      <div><%# Eval("ID") %></div> 
      <asp:Repeater ID="rpt_2" runat="server"/>     
     </ItemTemplate> 
    </asp:Repeater> 

    rpt_1.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound); 
    rpt_1.DataSource = q; 
    rpt_1.DataBind(); 

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (sender is Repeater) 
    { 
     string strID = ((Repeater)sender).ID.ToString(); 
     strID = strID.Substring(strID.Length - 1, 1).ToString(); 
     Repeater rpt = (Repeater)e.Item.FindControl("rpt_" + strID); 

     if (strID != "4") 
     {//Stop the nested creation 

     //Does this work? 
     //How do I accomplish this? 
     rpt.ItemTemplate = (Repeater)e.Item.FindControl("rpt_" + (Convert.ToInt32(strID)-1).ToString()).ItemTemplate; 

     rpt_1.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound); 
     rpt.DataSource = q; 
     rpt.DataBind(); 
     } 
    } 
} 

我可以將一箇中繼器的ItemTemplate中到另一個代碼?

回答

0

您可以創建在ASCX文件模板,然後加載它像這樣:

rpt_1.LoadTemplate(template_file.ascx); 

做到這一點,你打電話之前的DataBind();