当会话[“Role”]等于“Admin”时,它等于null

时间:2014-08-25 02:16:46

标签: c# webforms

在Login.aspx.cs中,我的btnLogin_Click函数看起来像这样

protected void btnLogin_Click(object sender, EventArgs e)
{
    if (LoginSuccessful(txtEmail.Text, txtPass.Text))
    {            
        Session["User"] = "Hacktivist";
        Response.Redirect("Default.aspx");
    }
    else
    {
        wrong.Visible = true;
    }
}

public Boolean LoginSuccessful(String EmailAddress, String Password)
{
    Boolean result = false;
    String Nickname;
    String Role;

    SqlCommand com = new SqlCommand(
        "select Nickname, Role, Password from Account where EmailAddress =     @EmailAddress", con);
    com.CommandType = CommandType.Text;
    com.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = EmailAddress;
    try
    {
        con.Open();
        SqlDataReader reader = com.ExecuteReader();
        while (reader.Read())
        {
            if (reader["Password"].ToString() == Helper.EncryptPassword(Password))
            {
                Nickname = reader["Nickname"].ToString();
                Role = reader["Role"].ToString();
                Session["Nickname"] = Nickname;
                Session["Role"] = Role;
                result = true;
                break;
            }
        }
    }
    catch
    {

    }
    finally
    {
        con.Close();
    }
    return result;
}

当管理员帐户成功登录后,它将被重定向到Default.aspx,而Default.aspx背后的代码看起来像这样

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["User"] != "Hacktivist") 
    {
        Session["Role"] = "Visitor";
    }

}

我将导航到Event.aspx,在那里我可以看到Edit HyperLink和btnAdd如果我是管理员,但问题是btnAdd仍然可见= false,即使我正在使用的帐户有一个Role = Admin。 .aspx代码如下所示:

 <div class="container marketing"> 
    <asp:Button ID="btnAdd" runat="server" Text="Create Event" Visible="false" OnClick="btnAdd_Click" />
  <!-- Three columns of text below the carousel -->
  <div class="row">
    <ul>
        <asp:ListView ID="lvEvent" runat="server">
            <ItemTemplate>
      <div class="col-lg-4">

                <li>
                    <img class="img-circle" src="<%# Eval("Poster") %>" alt="Generic placeholder image" style="width: 140px; height: 140px;">
                    <h2>
                    <a href="Event_Detail.aspx?EventID=<%# Eval("EventID") %>">
                        <%# Eval("Name") %>
                    </a>
                    </h2>
                    <h4>
                        <%# Eval("Venue") %> 
                    </h4>
                    <h5>
                        <%# Eval("Country") %>
                    </h5>
                    <h6>
                        <%# Eval("DateTimeStarted.Year") %> 
                    </h6>

                    <%if (Session["Role"].ToString() == "Admin")
                      { %>
                        <a href="Event_Edit.aspx?EventID=<%# Eval("EventID") %>">Edit</a>
                    <%} %>


                </li>

    </div><!-- /.col-lg-4 -->
    </ItemTemplate>
        </asp:ListView>
        </ul>
</div>

.cs代码如下所示:

public partial class Event : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(Helper.GetConnectionString());

    protected void Page_Load(object sender, EventArgs e)
    {
        GetEvents();

        if (Session["User"] == "Hacktivist")
        {
            if (Session["Role"] == "Admin")
            {
                btnAdd.Visible = true;
            }
        }

    }

    void GetEvents()
    {
        con.Open();
        SqlCommand com = new SqlCommand(
            "select * from event order by DateTimeStarted desc",
            con);
        SqlDataReader reader = com.ExecuteReader();
        lvEvent.DataSource = reader;
        lvEvent.DataBind();
        con.Close();
    } 

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Event_Add.aspx");
    }
}

感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

你试过吗

a)我认为虽然角色是管理员,但在案例中可能存在一些差异。只是为了确保不会导致问题,请尝试此Role.ToLower()。toString()==“admin”。

b)请确保您在Jquery或CSS中没有在page_load事件后禁用该按钮的代码。

希望它对你有所帮助。

谢谢