【发布时间】:2011-08-13 05:11:30
【问题描述】:
这是我的数据库
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.aspx, 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;
}
你能帮我正确填写菜单吗..
【问题讨论】:
-
如果您可以控制数据库,我建议让父列指向 ID,而不是文本列。这将允许您更改文件夹名称,而无需更新所有 parentitemid 单元格。
-
请不要将“C#”添加到您的标题中,只是为了将您的问题归类。我们在Stack Overflow 上使用标签。