我用Menu控件和数据库连接生成动态多级菜单,一级菜单出来了,但是二级的没有添加成功,后台程序是 5
publicpartialclass_Default:System.Web.UI.Page{publicinttreeID;protectedvoidPage_Load(...
public partial class _Default : System.Web.UI.Page
{
public int treeID;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection CON = new SqlConnection("server=LI;Trusted_Connection=SSPI;database=jpke;uid=sa;pwd=sasasasa;"); //dmenu是数据库名
//设定操纵数据库的命令
CON.Open();
SqlCommand Scmd = new SqlCommand(" select * from dmenu where ParentID=0 ", CON); //选出根菜单
SqlDataReader odr = Scmd.ExecuteReader();
while (odr.Read()) //循环读取
{
MenuItem menuNode = new MenuItem();
menuNode.Text = odr["Text"].ToString();
menuNode.Value = odr["ID"].ToString();
menuNode.Enabled = true;
menuNode.NavigateUrl = odr["Url"].ToString();
Menu1.Items.Add(menuNode); //添加到根菜单
treeID = Convert.ToInt32(menuNode.Value);
addchildmenu(menuNode);
}
//关闭数据库连接
Scmd.Connection.Close();
}
protected void addchildmenu(MenuItem pnode) //添加子菜单
{
SqlConnection CON = new SqlConnection("server=LI;Trusted_Connection=SSPI;database=jpke;uid=sa;pwd=sasasasa;");
//设定操纵数据库的命令
CON.Open();
SqlCommand Scmd1 = new SqlCommand(" select * from dmenu where ParentID="+treeID+"" , CON);
SqlDataReader odr = Scmd1.ExecuteReader();
while (odr.Read())
{
MenuItem menuNode = new MenuItem();
menuNode.Text = odr["Text"].ToString();
menuNode.Value = odr["ID"].ToString();
menuNode.Enabled = true;
menuNode.NavigateUrl = odr["Url"].ToString();
pnode.ChildItems.Add(menuNode); //为当前菜单项添加子菜单
treeID = Convert.ToInt32(menuNode.Value);
addchildmenu(menuNode); //循环添加子菜单
}
//关闭数据库连接
Scmd1.Connection.Close();
CON.Close();
CON.Dispose();
}
}
数据库中的字段意思:ID是编号,ParentID是父菜单号,Text是菜单名称,Url是连接页面地址 展开
{
public int treeID;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection CON = new SqlConnection("server=LI;Trusted_Connection=SSPI;database=jpke;uid=sa;pwd=sasasasa;"); //dmenu是数据库名
//设定操纵数据库的命令
CON.Open();
SqlCommand Scmd = new SqlCommand(" select * from dmenu where ParentID=0 ", CON); //选出根菜单
SqlDataReader odr = Scmd.ExecuteReader();
while (odr.Read()) //循环读取
{
MenuItem menuNode = new MenuItem();
menuNode.Text = odr["Text"].ToString();
menuNode.Value = odr["ID"].ToString();
menuNode.Enabled = true;
menuNode.NavigateUrl = odr["Url"].ToString();
Menu1.Items.Add(menuNode); //添加到根菜单
treeID = Convert.ToInt32(menuNode.Value);
addchildmenu(menuNode);
}
//关闭数据库连接
Scmd.Connection.Close();
}
protected void addchildmenu(MenuItem pnode) //添加子菜单
{
SqlConnection CON = new SqlConnection("server=LI;Trusted_Connection=SSPI;database=jpke;uid=sa;pwd=sasasasa;");
//设定操纵数据库的命令
CON.Open();
SqlCommand Scmd1 = new SqlCommand(" select * from dmenu where ParentID="+treeID+"" , CON);
SqlDataReader odr = Scmd1.ExecuteReader();
while (odr.Read())
{
MenuItem menuNode = new MenuItem();
menuNode.Text = odr["Text"].ToString();
menuNode.Value = odr["ID"].ToString();
menuNode.Enabled = true;
menuNode.NavigateUrl = odr["Url"].ToString();
pnode.ChildItems.Add(menuNode); //为当前菜单项添加子菜单
treeID = Convert.ToInt32(menuNode.Value);
addchildmenu(menuNode); //循环添加子菜单
}
//关闭数据库连接
Scmd1.Connection.Close();
CON.Close();
CON.Dispose();
}
}
数据库中的字段意思:ID是编号,ParentID是父菜单号,Text是菜单名称,Url是连接页面地址 展开
1个回答
展开全部
将protected void Page_Load(object sender, EventArgs e)里面的内容包含在
if (!IsPostBack)
{
SqlConnection CON = new SqlConnection("server=LI;Trusted_Connection=SSPI;database=jpke;uid=sa;pwd=sasasasa;"); //dmenu是数据库名
//设定操纵数据库的命令
CON.Open();
SqlCommand Scmd = new SqlCommand(" select * from dmenu where ParentID=0 ", CON); //选出根菜单
SqlDataReader odr = Scmd.ExecuteReader();
while (odr.Read()) //循环读取
{
MenuItem menuNode = new MenuItem();
menuNode.Text = odr["Text"].ToString();
menuNode.Value = odr["ID"].ToString();
menuNode.Enabled = true;
menuNode.NavigateUrl = odr["Url"].ToString();
Menu1.Items.Add(menuNode); //添加到根菜单
treeID = Convert.ToInt32(menuNode.Value);
addchildmenu(menuNode);
}
//关闭数据库连接
Scmd.Connection.Close();
}
中。
if (!IsPostBack)
{
SqlConnection CON = new SqlConnection("server=LI;Trusted_Connection=SSPI;database=jpke;uid=sa;pwd=sasasasa;"); //dmenu是数据库名
//设定操纵数据库的命令
CON.Open();
SqlCommand Scmd = new SqlCommand(" select * from dmenu where ParentID=0 ", CON); //选出根菜单
SqlDataReader odr = Scmd.ExecuteReader();
while (odr.Read()) //循环读取
{
MenuItem menuNode = new MenuItem();
menuNode.Text = odr["Text"].ToString();
menuNode.Value = odr["ID"].ToString();
menuNode.Enabled = true;
menuNode.NavigateUrl = odr["Url"].ToString();
Menu1.Items.Add(menuNode); //添加到根菜单
treeID = Convert.ToInt32(menuNode.Value);
addchildmenu(menuNode);
}
//关闭数据库连接
Scmd.Connection.Close();
}
中。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询