make usertype在标题中查看不同的内容

时间:2018-05-21 14:29:16

标签: c# asp.net webforms

我正在尝试使用asp.net创建一个网站。

我有不同用户的用户表,他们都有自己的用户类型,默认情况下是用户中的“U”。对于管理员来说,这是管理员的“A”。

我的网站也有标题部分。我希望我的代码能够读取哪个usertype当前有会话处于活动状态,并且如果它是“U”usertype则指定带有UserHome的标题菜单,或者如果它是“A”usertype则指定Adminpanel。

我该怎么做?我试过这种方式,但它没有用。

{

    String CS = 

ConfigurationManager.ConnectionStrings["BoothsConnectionString1"].ConnectionString;

    using (SqlConnection con = new SqlConnection(CS))

    {
        SqlCommand cmd = new SqlCommand("select * from Users", con);

        con.Open();

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        string Utype;
        Utype = dt.Rows[0][5].ToString().Trim();
        if (Utype == "U")
        {

            userhome.Visible = true;
            adminpanel.Visible = false;
        }
        if (Utype == "A")
        {
            adminpanel.Visible = true;
            userhome.Visible = false;
        }
   }
}

html部分:

<li><a href="adminhome.aspx" runat="server" id="admin">admin</a></li>
<li><a href="userhome.aspx" runat="server" id="user">user</a></li>

最新编辑:

 private void CheckUserType()
    {
            String CS = ConfigurationManager.ConnectionStrings["BoothsConnectionString1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("select Usertype from Users where usertype = @usertype", con);
                cmd.Parameters.Add("@usertype", SqlDbType.NChar).Value = Session["usertype"].ToString();
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);

                string Utype;
                Utype = dt.Rows[0][5].ToString().Trim();
                if (Utype == "U")
                {
                    user.Visible = true;
                    admin.Visible = false;
                }
                else if (Utype == "A")
                {
                    admin.Visible = true;
                    user.Visible = false;
                }
            }
        }

0 个答案:

没有答案
相关问题