简体   繁体   English

访问代码隐藏中的无序列表控件ID

[英]Access Unordered list control id's in codebehind

I have ul elements in usercontrol like below 我在usercontrol中有ul元素,如下所示

<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; foreach(子网站中的SPWeb超级站点){int i = 0; HtmlGenericControl li = new HtmlGenericControl("li"); 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. 在每个循环中,Current选项卡应更改为相应的下一个ul id。 How to access it? 如何访问它?

I have to generate 'li' elements dynamically under above ul's. 我必须在上面的ul下动态生成'li'元素。 So I need to access all the 'ul' id's one by one in the codebehind. 所以我需要在代码隐藏中逐个访问所有'ul'id。

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.. 我将在codebehind中实现一个集合,您可以通过presenter/controller或在page_load/click_events/..然后在ASP.NET MVC样式中进行简单循环。

// 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. 您可以使用Add方法创建自己的ListBuilder类,该方法采用自定义UserListItem类/结构。 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. 在这些类中,您可以使用TagBuilder使用您为UserListItem构建的自定义类/结构来创建LI和UL标记。 You could store a static dictionary of current Lists you're building in that custom ListBuilder class, using an user defined key. 您可以使用用户定义的密钥存储您在该自定义ListBuilder类中构建的当前列表的静态字典。

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. 这样,如果您需要从后面的代码中动态获取List或ListItems,您可以使用ListBuilder并通过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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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