2014-07-19 63 views
-2

asp.net中的登錄錯誤,名稱爲UserName的名稱在當前上下文中不存在,名稱Password在當前上下文中不存在。 Asp.codeasp.net中的登錄錯誤

<asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" Height="242px" Width="448px"> 
    <LayoutTemplate> 
     <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
      <tr> 
       <td> 
        <table cellpadding="0" style="height:242px;width:448px;"> 
         <tr> 
          <td align="center" colspan="2" style="color:White;background-color:#6B696B;font-weight:bold;">Log In</td> 
         </tr> 
         <tr> 
          <td align="right"> 
           <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label> 
          </td> 
          <td> 
           <asp:TextBox ID="UserName" runat="server"></asp:TextBox> 
           <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator> 
          </td> 
         </tr> 
         <tr> 
          <td align="right"> 
           <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label> 
          </td> 
          <td> 
           <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox> 
           <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2" align="center"> 
           <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." /> 
          </td> 
         </tr> 

         <tr> 
          <td align="center" colspan="2"> 
           <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" OnClick="LoginButton_Click" /> 
          </td> 
         </tr> 
        </table> 
       </td> 
      </tr> 
     </table> 
    </LayoutTemplate> 
    <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" /> 
</asp:Login> 

C#代碼

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Project_DBConnectionString"].ConnectionString) 

    SqlCommand cmd = new SqlCommand("select * from signin where Username [email protected] and [email protected]", con); 

    cmd.Parameters.AddWithValue("@username", UserName.Text); 
    cmd.Parameters.AddWithValue("@password", Password.Text); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataTable dt = new DataTable(); 
    da.Fill(dt); 
    if (dt.Rows.Count > 0) 
    { 
     Response.Redirect("Default.aspx"); 
    } 
    else 
    { 
     ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>"); 
    } 
+0

什麼是Username和Password和他們在哪裏定義? –

+1

請不要在**主題中編寫整個問題** – Shaharyar

+0

'在當前上下文中不存在'是編譯時錯誤。它發生在當你是一個超出範圍的對象或變量時。 – Shaharyar

回答

1

在登錄控制,你需要使用FindControl得到這兩個文本框,它們不是直接提供給頁面。

所以用它來訪問它們

TextBox TheUserName = Login1.FindControl("UserName") as TextBox; 
TextBox ThePassword = Login1.FindControl("Password") as TextBox; 

,然後用它們在你的代碼

cmd.Parameters.AddWithValue("@username", TheUserName.Text); 
cmd.Parameters.AddWithValue("@password", ThePassword.Text); 
+1

正常工作... – Mari

+0

@ user3797140所以請檢查答案是否正確,然後投票。 – Aristos