這是我的數據庫菜單出了錯
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)等等。但是,它又繼續完全錯誤和打印文件夾1(文件夾1),folder3(folder3),文件夾1(文件夾1)....
- 文件夾1(文件夾1)說,文件夾1是在括號中的菜單和文件夾1的子菜單。
這是我在代碼隱藏邏輯
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;
}
u能請幫助我在正確填寫菜單..
如果你有數據庫的控制,我會建議,而不是文本列具有父列指向ID, 。這將允許您更改文件夾名稱,而無需更新所有parentitemid單元格。 – Tyrsius
請不要將「C#」添加到您的標題中,以便將您的問題放入類別中。我們在[SO]上使用標籤。 –
@John:當然...... – user838359