Hi, so I have to create a dynamic menu which is created using <ul><li><a href /></li></ul> etc. The menu has to be dynamic based on the user who is viewing the page. So far when the page loads I send the name of the user logged in, to a stored procedure, the stored procedure then sends back a list of the menu items available for that user. I don't know how to code this so that the items are added to the list of items. Any pointers would be great. Thanks

Hi

Is this ASP.NET WebForms or MVC? The following approach (may not be the best) is based on WebForms as you mention page load and just presents the concept of adding your own markup from the code behind. If it is MVC then there are much easier ways of doing this.

In my aspx file I have the following markup:

    <body>
        <form id="form1" runat="server">
        <div id="listDiv" runat="server">

        </div>
        </form>
    </body>

Note that I have given the first div an id and told it to runat server so that it can be accessed in the code behind.

Then in the code behind, use a string builder to read your data from the database and build your <ul>:

    'You would substitute this and the for loop for your data returned from the database
    Dim items() As String = New String() {"Item 1", "Item 2", "Item 3"}
    Dim sb As New System.Text.StringBuilder

    sb.AppendLine("<ul>")

    If Not Page.IsPostBack Then

    For Each item As String In Items
        sb.AppendLine(String.Format("<li>{0}</li>", item))
    Next

    End If

    sb.AppendLine("</ul>")

    'Assign the string to the div
    listDiv.InnerHtml = sb.ToString

To be fair, this is not best practice as you are generating html markup in your code behind and is generally not recommended.

If this was MVC then your controller could request a List of the data (the model) to be used in the menu and pass this to your view. Your view could then enumerate that model and build the presentation code which is much more cleaner.

HTH

Just to add... asp.net does have a navigation menu control you can use and it can be dynamic in nature, but I really dont care for it. I would suggest djjeavons approach. The only difference i may suggest is that instead of using a div, i may just using a placeholder control.

Also, if you are reading from a database, you simply modify the process listed above for lines 9-11, but that should give you a good idea on how to handle this... assuming web forms.

Thank you so much,that works the way I want it to. Any ideas of how I can get it to display info related to the menu item selected using querystrings. The idea is that when one of the items is selected it will display text boxes for the user to enter details and then can generate a report based on the parameters entered. Each item in the menu will have a different list of parameters to be entered and I have to do it in a way where there is only one page so that the system can be flexible in the future.

Hi

When you build your <a href> you need to append the additional information to the link. For example:

<a href=Report.aspx?ReportID=" & yourID & "&SomeOtherValue=" & someOtherValue & "&AnotherValue=" & AnotherValue & ">LinkText</a>

Then, when Report.aspx page loads you can grab the query string values using Request.QueryString("ReportID") or Request.QueryString("SomeOtherValue")

HTH

Thank you :) Have it working, only problem seems to be is the value i'm passing in the query isn't one word but when I try to print the value to the screen as a label it only displays the first word.

Can you provide an example of the query string and how you are both setting it and reading it.

Yea I have the values stored in a list which is from a datatable populated via a stored procedure then I've used a foreach to send the right value when the link has been selected.

List<string> reports = new List<string>();

            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                reports.Add(datatable.Rows[i]["report_name"].ToString());
            }

            System.Text.StringBuilder sb = new StringBuilder();
            sb.AppendLine("<ul>");
            if (!Page.IsPostBack)
            {
                foreach (string item in reports)
                {
                        sb.AppendLine(string.Format("<li><a href = reports.aspx?ReportName=" +item + ">{0}</a></li>", item)); 
                }
            }

            sb.AppendLine("</ul>");
            listDiv.InnerHtml = sb.ToString();

To get the value on my other page I've used

 String reportName = Request.QueryString["ReportName"];
 Label2.Text = reportName;

Thanks that works perfectly :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.