2013-06-01 21 views
13

Tôi đang cố gắng theo dõi số example này về cách xác thực bằng chứng xác thực. Tuy nhiên, nó sử dụng asp: điều khiển cho biểu mẫu đăng nhập.Cách truy cập các điều khiển html trong mã phía sau

Nếu tôi được sử dụng điều khiển html thay vì phong cách CSS có thể được áp dụng, ví dụ như

<div id="login"> 
<a href="#" id="lclose"></a> 

     <form action="#" runat="server"> 
      <fieldset> 
       <div class="frame"> 
        <h4>Login</h4> 
        <small>Sign in to your account.</small> 
        <div class="clear"></div> 
        <input type="text" value="Username" class="input-text autoclear" /> 
        <input type="password" value="Password" class="input-text autoclear"/> 
       </div> 

       <div class="separator"></div> 

       <div> 
       <input type="submit" value="Sign in" class="input-submit float-right" runat="server" onserverclick="LoginButton_Click"/> 
       <a href="#" class="float-left">Forgot your password?</a> 
       </div> 

      </fieldset> 
     </form> 

</div> 

Làm thế nào để truy cập vào Username & Password trong mã đằng sau giống với?

protected void LoginButton_Click(object sender, EventArgs e) 
{ 
    // Validate the user against the Membership framework user store 
    if (Membership.ValidateUser(UserName.Text, Password.Text)) 
    { 
     // Log the user into the site 
     FormsAuthentication.RedirectFromLoginPage(UserName.Text, RememberMe.Checked); 
    } 
    // If we reach here, the user's credentials were invalid 
    InvalidCredentialsMessage.Visible = true; 
} 

Cú pháp chính xác thay vì UserName.Text, Password.Text là gì?

Trả lời

14

Thêm idrunat máy chủ thuộc tính cho thẻ đầu vào (xem dưới đây)

<input type="text" value="Username" class="input-text autoclear" id="Username" runat="server"/> 
<input type="password" value="Password" class="input-text autoclear" id="Password" runat="server"/> 

Bạn cũng cần thay đổi Text thành Value trong mã của bạn:

protected void LoginButton_Click(object sender, EventArgs e) 
{ 
    // Validate the user against the Membership framework user store 
    if (Membership.ValidateUser(Username.Value, Password.Value)) 
    { 
     // Log the user into the site 
     FormsAuthentication.RedirectFromLoginPage(UserName.Value, RememberMe.Checked); 
    } 
    // If we reach here, the user's credentials were invalid 
    InvalidCredentialsMessage.Visible = true; 
} 

Bạn cũng có thể thêm một html checkbox cho RememberMe

<input id="RememberMe" type="checkbox" runat="server" value ="RememberMe"/> 

Bây giờ bạn có thể kiểm tra trạng thái kiểm tra bằng cách gọi RememberMe.Checked

7

Thêm runat = "server" và id = "your_id" và bạn sẽ có quyền truy cập vào chúng. Ví dụ:

<input type="text" value="Username" class="input-text autoclear" 
    runat="server" id="UserName" /> 
<input type="password" value="Password" class="input-text autoclear" 
    runat="server" id="Password"/> 

Sau đó, bạn có thể truy cập các giá trị như thế này:

Membership.ValidateUser(UserName.Value, Password.Value) 
Các vấn đề liên quan