简体   繁体   中英

Access Unordered list control id's in codebehind

I have ul elements in usercontrol like below

<u><ul>
  <li id="liMiddle" class="off">
                    <a href="#">One
                    </a>
                    <ul id="One" runat="server">

                </ul>
              </li>
              <li id="liMiddle" class="off">
                <a href="#">Two
                </a>
                <ul id="Two" runat="server">

                </ul>
              </li>
              <li id="liMiddle" class="off">
                <a href="#">Three
                </a>
                <ul id="Three" runat="server">

                </ul>

foreach (SPWeb supersubsite in subsites) { int i = 0; HtmlGenericControl li = new HtmlGenericControl("li");

                        CurrentTab.Controls.Add(li);

                        HtmlGenericControl anchor = new HtmlGenericControl("a");
                        anchor.Attributes.Add("href", supersubsite.Url);
                        anchor.InnerText = supersubsite.Title;
                        li.Controls.Add(anchor);
                       } </u> 

Here in each loop Current tab should be changed to corresponding next ul id. How to access it?

I have to generate 'li' elements dynamically under above ul's. So I need to access all the 'ul' id's one by one in the codebehind.

Can anybody tell me the solution?

I would implement a collection in codebehind where you can add listitems by presenter/controller or in page_load/click_events/.. And then simple looping in an ASP.NET MVC style..

// YourPage.aspx.cs:
private readonly ICollection<string> items = new Collection<string> { "one", "two" };

// YourPage.aspx
<ul>
    <% foreach (var stringItem in this.items) { %>
    <li><%= stringItem %></li>
    <% } %>
</ul>

You could make your own ListBuilder class with an Add method that takes a custom UserListItem class/struct. Inside of these classes you could use TagBuilder to create the LI and UL tags using the custom class/struct you built for the UserListItem. You could store a static dictionary of current Lists you're building in that custom ListBuilder class, using an user defined key.

That way if you needed to get at your List, or ListItems dynamically from the code behind, you could just use your ListBuilder and reference them by ID.

Code below is a bit rough, but here's the general idea:

using System.Collections.Generic;
using System.Web.Mvc;
using System;

public class UserList
{
    public List<UserListItem> UserListItems = new List<UserListItem>();
    public void Add(UserListItem item)
    {
        UserListItems.Add(item);
    }
}

public class UserListItem
{
    public string Name { get; set; }
}


public class ListBuilder
{
    static Dictionary<string, UserList> userLists = new Dictionary<string, UserList>();
    public ListBuilder(string listId)
    {
        UserList newList = new UserList();
        newList.Add(new UserListItem() { Name = "Item1" });
        newList.Add(new UserListItem() { Name = "Item2" });
        userLists.Add(listId, newList);
    }
    public static UserList GetList(string listId)
    {
        return userLists[listId];
    }
    public static string BuildList(string listId)
    {
        UserList list = userLists[listId];
        TagBuilder listTagBuilder = new TagBuilder("ul");

        list.UserListItems.ForEach(listItem =>
        {
            TagBuilder listItemTagBuilder = new TagBuilder("li") { InnerHtml = listItem.Name };
            listTagBuilder.InnerHtml += listItemTagBuilder.ToString(TagRenderMode.Normal);
        });
        return listTagBuilder.ToString(TagRenderMode.Normal);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM