【问题标题】:how to make the drop down list to be visible for certain user only in asp.net?如何使下拉列表仅在 asp.net 中对某些用户可见?
【发布时间】:2023-04-10 14:33:01
【问题描述】:

我正在尝试使下拉列表仅对某些用户可见。例如,对于用户 = 1,我希望下拉列表可见,对于用户 = 2,我不希望下拉列表可见。

我已经试过了。

if ((Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 1) || 
     Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 2 || 
     Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 4)
        {
            ddlSpecialist.Visible = true;

            ddlSpecialist.DataSource = Lundbeck.Web.BusinessLogic.FrequencyReport.GetFrequencyReportbySpecialistList();
            ddlSpecialist.DataTextField = "Repcode";
            ddlSpecialist.DataValueField = "Repcode";
            ddlSpecialist.DataBind();
            ddlSpecialist.Items.Insert(0, new ListItem("All", "0"));
            ddlSpecialist.SelectedValue = "0";

            if (Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 3)
            {
                ddlSpecialist.Visible = false;
            }
        }

当我这样做时,我没有得到我想要的结果。这是为什么??提前致谢。

【问题讨论】:

  • 如果不希望对用户2可见,为什么在UserGroupId为2时设置为可见?:Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 2
  • 我认为你必须通过msdn.microsoft.com/en-us/library/5011f09h.aspx 才能知道 if else 语句是如何工作的

标签: asp.net sql-server role


【解决方案1】:

您要做什么,如果您正在检查条件,请查看 user==1 ||用户==2 ||用户==4 比您显示的下拉列表和相同的条件下您正在检查 user==3 如果 user==1 这怎么可能 ||用户==2 || user==4 而不是 ==3 像这样更改您的代码。

if ((Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 1) || 
 Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 2 || 
 Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 4)
    {
        ddlSpecialist.Visible = true;

        ddlSpecialist.DataSource = Lundbeck.Web.BusinessLogic.FrequencyReport.GetFrequencyReportbySpecialistList();
        ddlSpecialist.DataTextField = "Repcode";
        ddlSpecialist.DataValueField = "Repcode";
        ddlSpecialist.DataBind();
        ddlSpecialist.Items.Insert(0, new ListItem("All", "0"));
        ddlSpecialist.SelectedValue = "0";


    }
  else if (Convert.ToInt32(HttpContext.Current.Session["UserGroupId"]) == 3)
        {
            ddlSpecialist.Visible = false;
        }

【讨论】:

  • 谢谢@mairaj 它成功了。是的,我把它对用户可见是我的错误 = 2。我的错误。无论如何,谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 2018-01-07
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 2010-12-12
相关资源
最近更新 更多