如何更改两种不同可能性的失败文本?

时间:2014-02-10 16:09:35

标签: c# asp.net

我的页面中有 ASP登录控制

<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>

我想要做的是当我在

中执行身份验证时
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    DAL = new DataAccessLayer();
    conObj = DAL.openConnection();
    string query = "SELECT * FROM tblUser WHERE UEmail = '"+Login1.UserName+"'";

    SqlDataReader SQLDR = DAL.ExecuteQueryReturnValue(query);

    if (SQLDR.Read())
    {
        if (SQLDR["UPassword"].ToString() == Login1.Password.ToString())
        {

            Session["userName"] = SQLDR["UEmail"].ToString();
            e.Authenticated = true;
        }
        else {
            // Change the 'FailureText' to "password is incorrect"
            e.Authenticated = false;
        }

    }
    else
    {
        // Here I want to change the 'FailureText' to "User does not exists"
        e.Authenticated = false;
    }
   }

FailureText 的文本设置为

“密码不正确”

或设为

“用户不存在”

两种可能性。

2 个答案:

答案 0 :(得分:0)

试试这个:

if (SQLDR.HasRow())
{
    if(SQLDR.Read())
    {
        if (SQLDR["UEmail"].ToString() == Login1.UserName.ToString())
        {
            if (SQLDR["UPassword"].ToString() == Login1.Password.ToString())
            {
                Session["userName"] = SQLDR["UEmail"].ToString();
                e.Authenticated = true;
            }
            else
            {
                FailureText.Text = "password is incorrect";
                e.Authenticated = false;
            }
        }
    }
}
else
{
    FailureText.Text = "User does not exists";
    e.Authenticated = false;
}

<小时/> HasRow是检查表是否返回任何行的正确方法。

  

<强> HasRows
  获取一个值,该值指示是否为SqlDataReader   包含一行或多行。 (重写DbDataReader.HasRows。)

您还需要再添加一项检查,以确定电子邮件是否匹配,但密码是否匹配。


它是嵌套控件,然后在设置文本之前添加它:

var FailureText = ((Literal)loginControl.FindControl("FailureText"));

var FailureText = loginControl.FindControl("FailureText") as Literal;

答案 1 :(得分:0)

您可以在方法中声明一个字符串变量并相应地设置它:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    DAL = new DataAccessLayer();
    conObj = DAL.openConnection();
    string query = "SELECT * FROM tblUser WHERE UEmail = '"+Login1.UserName+"'";
    string failureText=string.empty;

    SqlDataReader SQLDR = DAL.ExecuteQueryReturnValue(query);

    if (SQLDR.Read())
    {
        if (SQLDR["UPassword"].ToString() == Login1.Password.ToString())
        {

            Session["userName"] = SQLDR["UEmail"].ToString();
            e.Authenticated = true;
        }
        else {
            // Change the 'FailureText' to "password is incorrect"
            failureText="password is incorrect";
            e.Authenticated = false;
        }

    }
    else
    {
        // Here I want to change the 'FailureText' to "User does not exists"
        failureText="User does not exists";
        e.Authenticated = false;
    }

    if(!e.Authenticated)
    {
        // Use failureText where you need it
        FailureText.Text=string.Format("<h1>{0}</h1>", failureText);
    }
    else
    {
        FailureText.Text=string.empty;
    }
   }

编辑:根据我从您那里获得的更多信息,我怀疑您正在尝试自定义登录控件。如果是这样,请设置Login1.FailureText = failureText