从数据库以编程方式禁用或启用菜单项

时间:2014-12-19 10:10:31

标签: c# asp.net

我想为数据库中的用户禁用或启用我的菜单栏,我已经为父菜单项做了,但我也想禁用或启用子菜单项,这里是我的代码

StrSql = "Select serial_no,optionName,MenuID from [User_" + Session["UserEmail"] + "]";

StrSql = StrSql + " Order By serial_no";

DataSet rs = (DataSet)MethodClass.ConnectionToQuery(StrSql);

if (rs.Tables[0].Rows.Count > 0)
{
    for (i = 0; i < MnuUserManagement.Items.Count; i++)
    {

        StrOpt = MnuUserManagement.Items[i].Text.ToString();

        if (StrOpt != "-" || StrOpt == "")
        {
            for (j = 0; j < rs.Tables[0].Rows.Count ; j++)
            {

                if (StrOpt == rs.Tables[0].Rows[j]["optionName"].ToString())
                {
                    MnuUserManagement.Items[i].Enabled = true;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

StrSql =“从[User_”+ Session [“UserEmail”] +“]”中选择serial_no,optionName,MenuID;               StrSql = StrSql +“Order By serial_no”;               DataSet rsmenu =(DataSet)MethodClass.ConnectionToQuery(StrSql);               if(rsmenu.Tables [0] .Rows.Count&gt; 0)               {

              for (var i = 0; i < rsmenu.Tables[0].Rows.Count; i++)
              {
                  foreach (MenuItem item in MnuUserManagement.Items)
                  {
       if (item.Text == rsmenu.Tables[0].Rows[i]["optionName"].ToString())
                      {
                          item.Enabled = true;

//这里我为子菜单项启动一个新的数据集 StrSql =“从[User_”+ Session [“UserEmail”] +“]”中选择serial_no,optionName,MenuID;             StrSql = StrSql +“Order By serial_no”;             DataSet rs =(DataSet)MethodClass.ConnectionToQuery(StrSql);

        if (rs.Tables[0].Rows.Count > 0)
        {
            for (var i = 0; i < rs.Tables[0].Rows.Count; i++)
            {

                foreach (MenuItem childItem in item.ChildItems)
                {
          if (childItem.Text == rs.Tables[0].Rows[i]["Optionname"].ToString())
                    {
                        childItem.Enabled = true;
                    }
                }
            }
        }
                      }
                  }
              }

          }