名称Login在当前上下文中不存在

时间:2016-10-10 09:16:49

标签: c# asp.net sql-server visual-studio stored-procedures

我无法弄清楚如何获取登录表单。我在登录页面中有登录控制,我收到如下消息:

(当前上下文中不存在名称Login1

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;

namespace User_Login
{
  public partial class Login : System.Web.UI.Page
  {
    protected void ValidateUser(object sender, EventArgs e)
    {
      int EMPLOYEE_ID = 0;
      string MyConnDB = ConfigurationManager.ConnectionStrings["MyConnDB"].ConnectionString;
      using (SqlConnection con = new SqlConnection(MyConnDB))
      {
        using (SqlCommand cmd = new SqlCommand("Validate_User"))
        {
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@USERNAME", Login1.UserName);
          cmd.Parameters.AddWithValue("@PASSWORD", Login1.Password);
          cmd.Connection = con;
          con.Open();
          EMPLOYEE_ID = Convert.ToInt32(cmd.ExecuteScalar());
          con.Close();
        }
        switch (EMPLOYEE_ID)
        {
          case -1:
            Login1.FailureText = "Username and/or password is incorrect.";
            break;
          case -2:
            Login1.FailureText = "Account has not been activated.";
            break;
          default:
            FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
            break;
          }
        }
      }
    }
  }

标记:

<asp:LoginView ID="LoginView1" runat="server">
    <LoggedInTemplate>
        <asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" DestinationPageUrl="~/Default.aspx" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" OnAuthenticate="ValidateUser">
            <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
            <LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
            <TextBoxStyle Font-Size="0.8em" />
            <TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
        </asp:Login>
    </LoggedInTemplate>
</asp:LoginView>

1 个答案:

答案 0 :(得分:0)

确保您在Asp前端代码

中插入了以下控件
<asp:Login ID = "Login1" runat = "server" OnAuthenticate= "ValidateUser"></asp:Login>