Menu went wrong
this is my database
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
I'm trying to build a menu out of this......
So it should look something like folder1(WebFrom1.aspx, WebForm2.aspx) and so on. But it went completely wrong and prints folder1(folder1), folder3(folder3),folder1(folder1) ....
- folder1 (folder 1) says that folder1 is the menu and folder1 in the brackets is the submenu.
This is my logic in codebehind
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")开发者_Python百科;
dst.Relations.Add("Children",
dst.Tables["Menu"].Columns["parentitemid"],
dst.Tables["Menu"].Columns["text"],false
);
return dst;
}
Can u please help me in filling the menu correctly..
It looks like your data retrieval is your first issue. It looks like you'll retrieve your parent data then immediately overwrite the Menu table with the child data. This then looks to relate your one table to itself on a one-to-one basis.
Try changing your Fills to different table names and work from there...
parent.Fill(dst, "Menu");
child.Fill(dst, "SubMenu");
精彩评论