动态生成Menu列表
我想把数据库中的数据,读取到软件menu的列表上,可以动态显示对象的名称。 --------------------编程问答-------------------- --------------------编程问答-------------------- if (dt.Rows.Count > 0){
foreach (DataRow dr in dt.Rows)
{
MenuItem Node = new MenuItem();
Node.Text = dr["categoryName"].ToString();
Node.NavigateUrl = "~/category.aspx?id=" + dr["categoryId"].ToString();
Node.Target = "_blank";
myMenu.Items.Add(Node);
}
} --------------------编程问答-------------------- 动态输出html --------------------编程问答-------------------- protected void Menu1_Load(object sender, EventArgs e)
{
try
{
dbhelp.con.Open();
string sql = string.Format("select menuName,id from dbo.PurviewManage where parentId=0");
SqlCommand com = new SqlCommand(sql, dbhelp.con);
SqlDataReader read = com.ExecuteReader();
for (int i = 0; i < 5; i++)
{
if (read.Read())
{
MenuItem t1 = new MenuItem();
Menu1.Items.Add(t1);
Menu1.Items[i].Text = read["menuName"].ToString();
Menu1.Items[i].Value = read["id"].ToString();
Menu1.Items[i].NavigateUrl = "leftFram.aspx?parentId=" + read["id"].ToString()+"&menuName="+read["menuName"].ToString()+"";
Menu1.Items[i].Target = "leftFrame";
Menu1.Items[i].SeparatorImageUrl = "~/resource/img/list_separator.gif";
}
}
}
catch (Exception wq)
{
Response.Write(wq.Message);
}
finally
{
dbhelp.con.Close();
}
}
}
这是我做的menu控件的加载事件的代码,菜单项是从数据库读取的,希望对你有用! --------------------编程问答-------------------- 循环递归拼接 --------------------编程问答--------------------
+1
子夜头像换了.
补充:.NET技术 , ASP.NET