Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态嵌套中继器ASP.net_C#_Repeater_Nested Repeater - Fatal编程技术网

C# 动态嵌套中继器ASP.net

C# 动态嵌套中继器ASP.net,c#,repeater,nested-repeater,C#,Repeater,Nested Repeater,我有一部分代码通过在HTML中硬编码来嵌套中继器: <asp:Repeater ID="rpt_1" runat="server"> <ItemTemplate> <div><%# Eval("ID") %></div> <asp:Repeater ID="rpt_2" runat="server"> <Item

我有一部分代码通过在HTML中硬编码来嵌套中继器:

    <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>

每个repeater的ItemTemplate都有完全相同的HTML和布局内容。有没有办法根据一个中继器的布局,通过编程将它们嵌套在n个中继器中?以下是我想做的PSEDOO代码:

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

rpt_1.ItemDataBound+=新的RepeaterItemEventHandler(rpt_ItemDataBound);
rpt_1.DataSource=q;
rpt_1.DataBind();
受保护的void rpt_ItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
if(发送方为中继器)
{
字符串strID=((中继器)发送器).ID.ToString();
strID=strID.Substring(strID.Length-1,1.ToString();
中继器rpt=(中继器)e.Item.FindControl(“rpt_”+strID);
如果(strID!=“4”)
{//停止嵌套创建
//这行吗?
//我如何做到这一点?
rpt.ItemTemplate=(Repeater)e.Item.FindControl(“rpt_”+(Convert.ToInt32(strID)-1.ToString()).ItemTemplate;
rpt_1.ItemDataBound+=新的RepeaterItemEventHandler(rpt_ItemDataBound);
rpt.DataSource=q;
rpt.DataBind();
}
}
}

我可以在代码中将一个中继器的ItemTemplate分配给另一个吗?

您可以在ascx文件中创建模板,然后按如下方式加载它:

rpt_1.LoadTemplate(template_file.ascx);
在调用DataBind()之前执行此操作