如何在asp中使用两个中继器

时间:2016-12-14 12:22:25

标签: c# asp.net repeater

我有一个菜单,我在asp中使用转发器,菜单的第一行正常工作,我想在菜单中添加第二个转发器的下拉选项,但是从课堂上我不需要&# 39;无权访问Repeater2

<asp:Repeater id="Repeater1"  runat="server" >
   <itemtemplate runat="server">
       <ul class="rmenue" runat="server">
           <li><a>
               <asp:Label  runat="server"  ID="Label1" 
                    text='<%# Eval("_name") %>' CommandArgument='<%# Eval("_url") %>' />

               <asp:Repeater id="Repeater2"  runat="server" >
                  <itemtemplate runat="server">
                     <ul class="menue" runat="server">
                        <li><a>
                           <asp:Label  runat="server"  ID="Label2" 
                                  text='<%# Eval("_name") %>' CommandArgument='<%# Eval("_url") %>' />
                        </a></li> 
                      </ul>
                   </itemtemplate>
               </asp:Repeater>
           </a></li> 
       </ul>
    </itemtemplate>
 </asp:Repeater>


public partial class Master : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        List<Buttons> ds = new List<Buttons>();
        ds.Add(new Buttons("Home", "blahbla"));
        ds.Add(new Buttons("ACCOUNTS", "blahbla"));
        ds.Add(new Buttons("ACCOUNTS", "blahbla"));
        ds.Add(new Buttons("PLAT", "blahbla"));
        ds.Add(new Buttons("EDU", "blahbla"));
        ds.Add(new Buttons("ABOUT", "blahbla"));
        ds.Add(new Buttons("CONTACT US", "blahbla"));


        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
}
public class Buttons
{

    public string _name { get; set; }
    public string _url { get; set; }
    public ArrayList<Buttons> drop = new ArrayList<Buttons>();\\this is the data for the second repeater
    public Buttons() { }
    public Buttons(string name,string url) {
        this._name = name;
        this._url = url;
    }

}

1 个答案:

答案 0 :(得分:0)

您可以实现此目的,但您需要在后面的代码中执行此操作。

在您的第一个中继器中,您需要OnItemDataBound个事件; OnItemDataBound="Repeater1_ItemDataBound"

在代码隐藏中:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var repeater2 = (Repeater)e.Item.FindControl("Repeater2");
        //now you can bind etc to repeater2
    }
}