菜单出了问题

本文关键字:问题 菜单 | 更新日期: 2023-09-27 18:05:41

这是我的数据库

   ID          parentitemid        text                Url
   1            NULL           folder1         /folder1
   2            folder1     WebForm1.aspx   /folder1/WebForm1.aspx
   3            folder1     WebForm2.aspx   /folder1/WebForm2.aspx
   6              null          folder3     /folder3
   7            folder3     WebForm1.aspx   /folder3/WebForm1.aspx
   8            folder3     WebForm2.aspx   /folder3/WebForm2.aspx
   9            folder3     WebForm3.aspx   /folder3/WebFomr3.aspx

我正试图建立一个菜单的......

所以它应该看起来像folder1(WebFrom1)。如WebForm2.aspx等。但是它完全出错了,打印了folder1(folder1), folder3(folder3),folder1(folder1) ....

  • folder1(文件夹1)表示folder1是菜单,括号中的folder1是子菜单。

这是我在代码后面的逻辑

  if (!IsPostBack)
{
    DataSet dst = GetMenuData();
    foreach (DataRow masterRow in dst.Tables["Menu"].Rows)
    {
        if ((string)masterRow["parentitemid"] != "NULL" ||
            (string)masterRow["parentitemid"] != "")
        {
            MenuItem masterItem = new MenuItem((string)masterRow["parentitemid"]);
            Menu1.Items.Add(masterItem);
            foreach (DataRow childRow in masterRow.GetChildRows("Menu"))
            {
                MenuItem childItem = new MenuItem((string)childRow["text"]);
                masterItem.ChildItems.Add(childItem);
            }
        }
    }
}
  DataSet GetMenuData()
    {
        string connectionString = "Data Source=NISHANTH-PC''SQLEXPRESS;Initial     
   Catalog=roletesting;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectionString);
        SqlDataAdapter parent = new SqlDataAdapter("Select DISTINCT parentitemid from 
    Menu", con);
        SqlDataAdapter child = new SqlDataAdapter("SELECT * FROM Menu", con);
        DataSet dst = new DataSet();
        parent.Fill(dst, "Menu");
        child.Fill(dst, "Menu");
        dst.Relations.Add("Children",
            dst.Tables["Menu"].Columns["parentitemid"],
        dst.Tables["Menu"].Columns["text"],false
      );
        return dst;
    }

你能帮我正确填写菜单吗

菜单出了问题

看起来你的数据检索是你的第一个问题。看起来您将检索父数据,然后立即用子数据覆盖Menu表。这看起来是在一对一的基础上将您的一个表与其本身关联起来。

试着改变你的填充到不同的表名和工作从那里…

        parent.Fill(dst, "Menu");
        child.Fill(dst, "SubMenu");