c#中的构建菜单
本文关键字:菜单 构建 | 更新日期: 2023-09-27 18:05:19
我是这样做的:
<asp:Menu
id="Menu1"
Orientation="Horizontal"
Runat="Server">
<StaticMenuStyle
BackColor="#eeeeee"
BorderStyle="solid"
BorderColor="Black"
BorderWidth="1" />
<DynamicMenuStyle
BackColor="#eeeeee"
BorderStyle="solid"
BorderColor="Black"
BorderWidth="1" />
</asp:Menu>
后面的代码:
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["ID"]);
masterItem.ChildItems.Add(childItem);
}
}
}
}
基本上,我试图从数据库绑定菜单。然而,我得到这个错误:
这是我的数据库表:无法强制转换"System"类型的对象。
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
那么,你能帮我填一下菜单吗
在检查DataTable
中的null时执行如下操作if(dataRow["colName"] != DBNull.Value)
{
// logic
}
类似:
if(masterRow["parentitemid"] != DBNull.Value &&
!string.IsNullOrEmpty(masterRow["parentitemid"].ToString())
看到DBNull。