• 关于Repeater控件的一个问题


    用Repeater生成一个树控件,想把子节点在后台代码中动态绑定,可是代码不能按预期的执行,请教大家帮忙分析一下是什么原因呢?

    代码说明如下: 

    aspx页面代码:

    <div id="divParty">
        <ul class="tree treeFolder" >
            <asp:Repeater ID="partyRepeater" OnItemDataBound="partyRepeater_ItemDataBound" runat="server" >
            <ItemTemplate>        
            <li><a href="javascript:;" id="partyLink" runat="server"></a></li>
            </ItemTemplate>
            </asp:Repeater>
           </ul>

    </div> 

     aspx.cs代码:

    protected void partyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) {
                PartyEntity party = e.Item.DataItem as PartyEntity;
                if (party == null)
                    return;
                
                HtmlAnchor a = e.Item.FindControl("partyLink"as HtmlAnchor;
                if (a != null) {
                    a.InnerText = party.Title.Trim();
                    a.Attributes.Add("onclick""loadNode("+ party.ID +")");
                }

                List<PartyEntity> children = GetChildren(party.ID);
                if (children == null || children.Count == 0)
                    return;

                HtmlGenericControl ul = new HtmlGenericControl("UL");
                e.Item.Controls.Add(ul);

                // 绑定子节点
                Repeater rep = new Repeater();
                rep.ItemTemplate = new PartyTemplate();
                rep.ItemDataBound += new RepeaterItemEventHandler(partyRepeater_ItemDataBound);
                rep.DataSource = children;
                rep.DataBind();
                ul.Controls.Add(rep);

     期望的结果:黄色高亮部分作为子节点,放在li标签内;

    <UL class="tree treeFolder" >
    <LI>
    <DIV >
    <DIV class=node></DIV>
    <DIV class=file></DIV><A id=partyLink onclick=loadNode(1) href="javascript:;" >父节点</A></DIV>
    <UL >
    <LI><A onclick=loadNode(2>子节点1</A></LI>

    <LI><A onclick=loadNode(3>子节点2</A></LI> 

    </UL>
    </LI>
    </UL>

     实际运行结果,总是在li标签外:

     <UL class="tree treeFolder" > 

    <LI>
    <DIV >
    <DIV class=node></DIV>
    <DIV class=file></DIV><A id=partyLink onclick=loadNode(1) href="javascript:;" >

    父节点</A></DIV>
    </LI>

    <UL>

    <LI><A onclick=loadNode(2>子节点1</A></LI>

    <LI><A onclick=loadNode(3>子节点2</A></LI> 

    </UL> 
    </UL> 

      


  • 相关阅读:
    OpenJudge 2721 忽略大小写比较字符串大小
    Poj 2586 / OpenJudge 2586 Y2K Accounting Bug
    Poj 2109 / OpenJudge 2109 Power of Cryptography
    Poj 1328 / OpenJudge 1328 Radar Installation
    Poj 2965 The Pilots Brothers' refrigerator
    Poj 2503 / OpenJudge 2503 Babelfish
    Poj 2388 Who's in the Middle
    模板:cin.getline用法
    Poj 3094 Quicksum
    Charles-Mac安装、配置及苹果手机安装证书
  • 原文地址:https://www.cnblogs.com/seesky/p/2285041.html
Copyright © 2020-2023  润新知