How to access html controls in code behind

后端 未结 3 1303
一向
一向 2020-12-17 16:05

I\'m trying to follow this example on how to validate credentials. However, it uses asp: controls for the login form.

If I were to use html controls instead so CSS s

相关标签:
3条回答
  • 2020-12-17 16:38

    Add runat="server" and id="your_id" and you should have access to them. For example:

    <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"/>
    

    Then you can access the values like this:

    Membership.ValidateUser(UserName.Value, Password.Value)
    
    0 讨论(0)
  • 2020-12-17 16:40

    You can access them from code behind by adding runat="server" on the html elements.

    http://www.w3schools.com/aspnet/aspnet_refhtmlcontrols.asp

    The below link has an example of how you can do this

    http://msdn.microsoft.com/en-us/library/aa478973.aspx

    0 讨论(0)
  • 2020-12-17 16:58

    Add id and runat server attributes to the input tag (see below)

    <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"/>
    

    You also need to change Text to Value in your code:

    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;
    }
    

    You can also add a html checkbox for RememberMe

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

    Now you can check the checked states by calling RememberMe.Checked

    0 讨论(0)
提交回复
热议问题