locked
Add a Menu Item RRS feed

  • Question

  • User-309523270 posted

    Hi,

    How do you add a menu item in the menu list?

    I'm using Visual Studio 2010, Dynamic Data 4. 

    Thanks,

    TinaC99 

    Thursday, April 14, 2011 1:40 PM

Answers

  • User-330204900 posted

    Hi Tinac99, if you look at the Default.aspx.cs page,

    protected void Page_Load(object sender, EventArgs e)
    {
        //System.Collections.IList visibleTables = Global.DefaultModel.VisibleTables.ToList();
        var visibleTables = from t in Global.DefaultModel.VisibleTables
                            orderby t.Attributes.OfType<TableOrderAttribute>().FirstOrDefault(), t.Name
                            select t;
    
        if (visibleTables.Count() == 0)
        {
            throw new InvalidOperationException("There are no accessible tables. Make sure that at least...");
        }
        Menu1.DataSource = visibleTables;
        Menu1.DataBind();
    }

    and the mark-up:

    <asp:GridView ID="Menu1" runat="server" AutoGenerateColumns="false"
        CssClass="DDGridView" RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6">
        <Columns>
            <asp:TemplateField HeaderText="Table Name"> 
                <ItemTemplate>
                    <asp:DynamicHyperLink ID="HyperLink1" runat="server"><%# Eval("DisplayName") %></asp:DynamicHyperLink>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    ?You can see that because they are usnign DynamicHyperLink it would be difficult to simply add items to the list you would need to change the DynamicHyperLink for a standard HyperLink and do the heavey lifting your self somthin like this:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //System.Collections.IList visibleTables = Global.DefaultModel.VisibleTables.ToList();
            var visibleTables = (from t in Global.DefaultModel.VisibleTables
                                orderby t.Attributes.OfType<TableOrderAttribute>().FirstOrDefault(), t.Name
                                select new MenuItem { DisplayName = t.DisplayName, Url = t.ListActionPath }).ToList();
    
            if (visibleTables.Count() == 0)
            {
                throw new InvalidOperationException("There are no accessible tables. Make sure that at least one data model...");
            }
    
            visibleTables.Add(new MenuItem() { DisplayName = "Test Item", Url = "~/Default.aspx" });
            Menu1.DataSource = visibleTables;
            Menu1.DataBind();
        }
    }
    
    class MenuItem
    {
        public String DisplayName { get; set; }
        public String Url { get; set; }
    }

    Adn update the mark-up like this, change the DynamicHyperLink to:

    <asp:HyperLink ID="DynamicHyperLink1" NavigateUrl='<%# Eval("Url") %>' runat="server"><%# Eval("DisplayName") %></asp:HyperLink>

    That should do the trick or also have a look at my menu sample here SiteMapProvider for Dynamic Data (.Net 3.5 SP1 and .Net 4.0)

    

    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 16, 2011 2:53 PM

All replies

  • User-718146471 posted

    That depends how the menu items are being retrieved. Are you using a database? If so, add more items to the database table where these items live.

    Thursday, April 14, 2011 4:19 PM
  • User-309523270 posted

    Thanks for your reply.  I decided to change tactics and overwrote the Default.aspx to add another table below the menu item list.

    The table cell has an anchor

    < a href="Reports/ReportStrikeSubjectFollowup.aspx">Strike Follow-Up</a>

    The Reports is a new folder I added under CustomPages folder. 

    Also in the Global.asax, I also added this:

    routes.Add("ReportStrikeSubjectFollowUp", new DynamicDataRoute("Reports/ReportStrikeSubjectFollowup.aspx")

    Constraints = new RouteValueDictionary(new { action = "ReportStrikeSubjectFollowup"

    }), Model = DefaultModel 

     

    });

     

     Back to the <a href="..."> tag, I have tried:

    "~/Reports/ReportStrikeSubjectFollowup.aspx"

     

    "../Reports/ReportStrikeSubjectFollowup.aspx"

    "/Reports/ReportStrikeSubjectFollowup.aspx"

    But all of these generated the same result:

    The resource cannot be found.  HTTP 404.  The resource you are looking for(or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable...

    What do I do to make the screen show?

    Thanks,

    TinaC99

    Thursday, April 14, 2011 6:53 PM
  • User-718146471 posted

    The Reports is a new folder I added under CustomPages folder. 

    Ok, if you have this under a CustomPages folder then make sure that it is included when you do your reference like this:

    "~/CustomPages/Reports/ReportStrikeSubjectFollowup.aspx"

     If you don't do that, the site will think you mean "~/Reports/ReportStrikeSubjectFollowup.aspx" as "http://www.mysite.com/reports/reportstrikesubjectfollowup.aspx"

    Friday, April 15, 2011 8:11 AM
  • User-330204900 posted

    Do you mean the list of tables on the Default page?

    Friday, April 15, 2011 9:52 AM
  • User-309523270 posted

    Yes, sir.  The list of tables on the Default page.

    Friday, April 15, 2011 6:23 PM
  • User-309523270 posted

    Thanks for the reply.  It is still showing up the same message.

    Friday, April 15, 2011 7:04 PM
  • User-330204900 posted

    Hi Tinac99, if you look at the Default.aspx.cs page,

    protected void Page_Load(object sender, EventArgs e)
    {
        //System.Collections.IList visibleTables = Global.DefaultModel.VisibleTables.ToList();
        var visibleTables = from t in Global.DefaultModel.VisibleTables
                            orderby t.Attributes.OfType<TableOrderAttribute>().FirstOrDefault(), t.Name
                            select t;
    
        if (visibleTables.Count() == 0)
        {
            throw new InvalidOperationException("There are no accessible tables. Make sure that at least...");
        }
        Menu1.DataSource = visibleTables;
        Menu1.DataBind();
    }

    and the mark-up:

    <asp:GridView ID="Menu1" runat="server" AutoGenerateColumns="false"
        CssClass="DDGridView" RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6">
        <Columns>
            <asp:TemplateField HeaderText="Table Name"> 
                <ItemTemplate>
                    <asp:DynamicHyperLink ID="HyperLink1" runat="server"><%# Eval("DisplayName") %></asp:DynamicHyperLink>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    ?You can see that because they are usnign DynamicHyperLink it would be difficult to simply add items to the list you would need to change the DynamicHyperLink for a standard HyperLink and do the heavey lifting your self somthin like this:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //System.Collections.IList visibleTables = Global.DefaultModel.VisibleTables.ToList();
            var visibleTables = (from t in Global.DefaultModel.VisibleTables
                                orderby t.Attributes.OfType<TableOrderAttribute>().FirstOrDefault(), t.Name
                                select new MenuItem { DisplayName = t.DisplayName, Url = t.ListActionPath }).ToList();
    
            if (visibleTables.Count() == 0)
            {
                throw new InvalidOperationException("There are no accessible tables. Make sure that at least one data model...");
            }
    
            visibleTables.Add(new MenuItem() { DisplayName = "Test Item", Url = "~/Default.aspx" });
            Menu1.DataSource = visibleTables;
            Menu1.DataBind();
        }
    }
    
    class MenuItem
    {
        public String DisplayName { get; set; }
        public String Url { get; set; }
    }

    Adn update the mark-up like this, change the DynamicHyperLink to:

    <asp:HyperLink ID="DynamicHyperLink1" NavigateUrl='<%# Eval("Url") %>' runat="server"><%# Eval("DisplayName") %></asp:HyperLink>

    That should do the trick or also have a look at my menu sample here SiteMapProvider for Dynamic Data (.Net 3.5 SP1 and .Net 4.0)

    

    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 16, 2011 2:53 PM