Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 styles can be applied, eg

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

How do I access the Username & Password in code behind similar to?

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

What is the correct syntax instead of UserName.Text, Password.Text?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
228 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...