How to work with two repeaters in asp

924 views Asked by At

I have a menu that I'm making with a repeater in asp and the first row of the menu is working and I want to add drop down options in the menu with the second repeater but from the class I don't have access to 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

There are 1 answers

2
Hugo Yates On BEST ANSWER

You can achieve this but you need to do it in the code behind.

In your 1st Repeater you need an OnItemDataBound event; OnItemDataBound="Repeater1_ItemDataBound"

In the codebehind:

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