根据<ul>的动态ID将<li>标记移至其各自的<ul>

时间:2019-02-13 06:06:28

标签: c# asp.net

我正在创建动态两级边的列表,但是我被困在寻找基于ID将li绑定到其各自ul中的方法。

像往常一样,我检索了形成动态侧级菜单所需的所有数据,该菜单将放置在名为“ accordion”的div ID内,不带parentID的menuID的名称将设置为h3,后跟div标签用其ID覆盖ul

带有parentID的menuID将基于li的父ID ul在li内部设置。但是,它似乎以在ul之外创建的li结束。

下面将是我的PopulateMenu()函数

private void PopulateMenu(DataTable dt, string Lvl4, MenuItem parentMenuItem)
    {
        string currentPage = Path.GetFileName(Request.Url.AbsolutePath);



        foreach (DataRow row in dt.Rows)
        {

            string url = Convert.ToString(row["fullPath"]);
            url = url.Replace(">", "/");
            HtmlGenericControl h3 = new HtmlGenericControl("h3");
            HtmlGenericControl li = new HtmlGenericControl("li");
            HtmlGenericControl div = new HtmlGenericControl("div");
            HtmlGenericControl anchor = new HtmlGenericControl("a");
            HtmlGenericControl ul = new HtmlGenericControl("ul");


//This are the expression to produce the value to be compare with the ID of the menu to see whether they will be parent or child menu item.

            string ComparePageID = row["PageID"].ToString();
            string CompareLvl3 = "0000";
            string CompareLvl4 = CompareLvl3.Replace("0000", "");

            if (ComparePageID.Contains(CompareLvl3))
            {
                h3.InnerText = row["Description"].ToString();

//I tried to use the session to save the ul id like  
//Session["sidemenu"] = row["PageID"];

                ul.Attributes.Add("id", row["PageID"].ToString());
                ul.Attributes.Add("runat", "server");
                div.Attributes.Add("id", "server");
                div.Attributes.Add("runat", "server");
                accordion.Controls.Add(h3);
                accordion.Controls.Add(div);
                div.Controls.Add(ul);
            }
            else
            {
                anchor.Attributes.Add("href", "~/html/" + url + ".aspx");
                anchor.Attributes.Add("id", row["PageID"].ToString());
                anchor.Attributes.Add("target", "manualmain");
                anchor.Attributes.Add("runat", "server");
                anchor.InnerText = row["Description"].ToString();
                li.Controls.Add(anchor);

 //But when I use the value of Session["sidemenu"] at here to add the child item in li, error "the XXX does not exist in the current context" occurred

                accordion.Controls.Add(li);


            }


        }
    }

我的预期输出将是     

<h3>Parent 1</h3>
<div>
<ul id = "Somewhere0000">
    <li><a href="somewhere.aspx" id ="Somewhere0001">Child 1</a></li>
    <li><a href="somewhere2.aspx" id ="Somewhere0002">Child 2</a></li>
</ul>
</div>

<h3>Parent 2</h3>
<div>
<ul id = "SomeHow0000">
    <li><a href="SomeHow.aspx" id ="SomeHow0001">Child 1</a></li>
    <li><a href="SomeHow2.aspx" id ="SomeHow0002">Child 2</a></li>
</ul>
</div>

</div>

但是实际结果是

<div id="accordion" style="width:100%; vertical-align:middle; padding:0px 0px 0px 0px;">
<h3>Parent 1</h3>
<div>
<ul id = "Somewhere0000"></ul>
</div>

<li><a href="somewhere.aspx" id ="Somewhere0001">Child 1</a></li>

<li><a href="somewhere2.aspx" id ="Somewhere0002">Child 2</a></li>

<h3>Parent 2</h3>
<div>
<ul id = "SomeHow0000"></ul>
</div>              

<li><a href="SomeHow.aspx" id ="SomeHow0001">Child 1</a></li>

<li><a href="SomeHow2.aspx" id ="SomeHow0002">Child 2</a></li>

 </div>

因此,我现在正在寻找将动态li推入各自动态ul中的另一种方法。无论是C#还是javascript。...

1 个答案:

答案 0 :(得分:0)

accordion.Controls.Add(li);行中,将li直接添加到手风琴中。这导致您描述的结果。

您必须将li添加到ul中。为此,您需要存储最后创建的ul。在以下示例中,我用// >>>

标记了相关的更改
private void PopulateMenu(DataTable dt, string Lvl4, MenuItem parentMenuItem)
{
  string currentPage = Path.GetFileName(Request.Url.AbsolutePath);
  foreach (DataRow row in dt.Rows)
  {
    string url = Convert.ToString(row["fullPath"]);
    url = url.Replace(">", "/");
    HtmlGenericControl h3 = new HtmlGenericControl("h3");
    HtmlGenericControl li = new HtmlGenericControl("li");
    HtmlGenericControl div = new HtmlGenericControl("div");
    HtmlGenericControl anchor = new HtmlGenericControl("a");
    // >>> Instead of generating a new ul for every row, only generate it if you add a new header
    HtmlGenericControl ul;
   //This are the expression to produce the value to be compare with the ID of the menu to see whether they will be parent or child menu item.
   string ComparePageID = row["PageID"].ToString();
   string CompareLvl3 = "0000";
   string CompareLvl4 = CompareLvl3.Replace("0000", "");
   if (ComparePageID.Contains(CompareLvl3))
   {
     h3.InnerText = row["Description"].ToString();
     //I tried to use the session to save the ul id like  
     //Session["sidemenu"] = row["PageID"];
     // >>> Create new ul only if header is created
     ul = new HtmlGenericControl("ul");
     ul.Attributes.Add("id", row["PageID"].ToString());
     ul.Attributes.Add("runat", "server");
     div.Attributes.Add("id", "server");
     div.Attributes.Add("runat", "server");
     accordion.Controls.Add(h3);
     accordion.Controls.Add(div);
     div.Controls.Add(ul);
   }
   else
   {
     anchor.Attributes.Add("href", "~/html/" + url + ".aspx");
     anchor.Attributes.Add("id", row["PageID"].ToString());
     anchor.Attributes.Add("target", "manualmain");
     anchor.Attributes.Add("runat", "server");
     anchor.InnerText = row["Description"].ToString();
     li.Controls.Add(anchor);
     //But when I use the value of Session["sidemenu"] at here to add the child item in li, error "the XXX does not exist in the current context" occurred
     // >>> Add li to ul
     ul.Controls.Add(li);
   }
 }

}


但是,使用Repeater控件可以轻松解决您描述的情况。首先,在ASP.NET WebForms中动态创建控件似乎很简单,但要使其在各个方面都能正常工作,确实很复杂。因此,我不建议在此动态解决方案中花费很多精力,而强烈建议您查看Repeater控件以及如何将其用于生成所需的标记。