15

我正在尝试按照此示例了解如何验证凭据。但是,它使用 asp: 控制登录表单。

如果我要使用 html 控件,以便可以应用 CSS 样式,例如

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

如何在类似于后面的代码中访问用户名和密码?

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

什么是正确的语法而不是UserName.Text, Password.Text

4

3 回答 3

17

向输入标签添加id和服务器属性(见下文)runat

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

您还需要在代码中更改TextValue

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

您还可checkbox以为RememberMe

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

现在您可以通过调用检查检查状态RememberMe.Checked

于 2013-06-01T17:31:18.640 回答
7

添加 runat="server" 和 id="your_id" 你应该可以访问它们。例如:

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

然后你可以访问这样的值:

Membership.ValidateUser(UserName.Value, Password.Value)
于 2013-06-01T17:32:03.590 回答
3

runat="server"您可以通过添加html 元素从后面的代码中访问它们。

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

下面的链接有一个例子,你可以如何做到这一点

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

于 2013-06-01T17:27:51.350 回答