views:

92

answers:

1

Hey,

I have a custom user control in ASP.net:

MenuButton.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/MenuButton.ascx.cs" Inherits="MenuButton" ClassName="MenuButton" %>

<li><a href="<%= Link %>"><span><%= Text %></span></a></li>

MenuButton.ascx.cs:

using System;

public partial class MenuButton : System.Web.UI.UserControl
{
    public String Link = "test.html";
    public String Text = "TEST";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

MasterPage.master has this:

<%@ Reference Control="~/MenuButton.ascx" %>
<%@ Register Src="~/MenuButton.ascx" TagName="Menu" TagPrefix="button" %>


<ul runat="server" id="Menu"></ul>

MasterPage.master.cs

public List<MenuButton> Menus = new List<MenuButton>();
protected void Page_Load(object sender, EventArgs e)
{

    foreach (var control in Menus)
    {
        Menu.Controls.Add(control);
    }
}

Default.aspx

Master.Menus.Add( new MenuButton { Text = "Test1", Link = "test1.html" });

It compiles OK, even with the debugger it does what it should do but they just don't appear on my page. Am I missing something here?

Thanks.


Edit 1:

I tried to override OnInit on my master page:

protected override void OnInit(EventArgs e)
{
 base.OnInit(e);

    foreach (var control in SousMenus)
 {
  SousMenu.Controls.Add(control);
 }
}

It didn't work also.

A: 

I think you have to add your new buttons in the Page_Init method.

// EDIT: I just tested this, but it doesn´t work.

philipproplesch