1

I am trying to have an error message displayed when a username and password are not recognized.

if (rdr.Read())
{
    int id = int.Parse(rdr.GetValue(0).ToString());
    string fname = rdr.GetString(1);

    Session["ID"] = id;
    Session["FName"] = fname;
    con.Close();
    Response.Redirect("Home.aspx");
}
else 
{
    Response.Redirect("Login.aspx?err='blabla'"); //Display message
}

The following code (Page_Load) is supposed to be invoked in the else statement, but it is not:

public partial class _Default : System.Web.UI.Page
{
    protected string err = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form.Count > 0)
        {
            err = Request.Form["err"];
        }
    }
}

Why is this the case? Thank you all so much!

4

2 回答 2

2

This is a GET value in the query string, not a POST value in the form. You can use Request.QueryString[] or Request[] which contains POST and GET values.

if (Request.QueryString.Count > 0)
{
    err = Request.QueryString["err"];
}

or

if (Request.Count > 0)
{
    err = Request["err"];
}

Also, the query string value belongs to the Login page, so you won't be able to access it from _Default. Move your Page_Load logic to Login.aspx.cs.

于 2013-03-27T20:16:55.247 回答
0

一般来说,根据类名_Default,我相信你已经把这段代码放在了Default.aspx页面中。将代码放在Load页面Login.aspx中。然后也按照jrummell提供的方向。

于 2013-03-27T20:19:02.400 回答