Вопрос

I'm trying to build a juiceui-menu at runtime when the page loads but I'm not having any luck. It works just fine if I build the menu at design time but need to build it at runtime. Any help would be much appreciated! Here's the code I have so far...

c# code:

    private void BuildLoginMenu()
        {
            Juice.Menu jMenuContainer = new Juice.Menu();

            Juice.MenuItem parentItem = new Juice.MenuItem();
            parentItem.ID = "Parent1";
            MenuTemplate p = new MenuTemplate("Parent");
            parentItem.Content = p;      

            Juice.MenuItem childItem = new Juice.MenuItem();
            childItem.ID = "Parent1";
            MenuTemplate c = new MenuTemplate("Parent");
            childItem.Content = c;

            parentItem.Items.Add(childItem);

            jMenuContainer.Items.Add(parentItem);
            Placeholder1.Controls.Add(jMenuContainer);
      }
    }

here's the MenuTemplate class for reference:

    public class MenuTemplate : ITemplate
        {

            private string _text;

            public MenuTemplate(string text)
            {
              _text = text;
            }

           public void InstantiateIn(Control container)
           {
             LiteralControl l = new LiteralControl(_text);
             container.Controls.Add(l);
           }
       }
Это было полезно?

Решение

I'm answering my own question this time around... What I found to be easier to do is to avoid building the JuiceUI:Menu in the code-behind. Instead build a HTML unordered list using the jQueryUI CSS files to style the list. Since jQueryUI is the backbone of JuiceUI, I had no issue getting this to work. Here's my reasoning why I went this route: You can't render server controls in code-behind as a string and assign the output to a div's innerhtml property, but you can render html as a string in the code-behind and assign it to a div's innerhtml property - this allows me to dynamically build the menu. See below for the code that works for me.

HTML

        <script type="text/javascript">
          $(function () {
               $("#menu").menu();
          });
        </script>    

        <div id="menuDiv" runat="server">
        </div>

C#

        str += "<ul id=\"menu\">  ";        
        //loop through each menuItem, build menu from top down.
        foreach (MenuItem m in menuItems)
        {        

            str += "<li>";
            str += "<a href=\"#\">" + m.Title + "</a>";
            str += "</li>";           

        }
        str += "</ul>";
        menuDiv.InnerHtml = str;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top