如何访问HTML控件在code背后 [英] How to access html controls in code behind
问题描述
我想按照这个<一个href=\"http://www.asp.net/web-forms/tutorials/security/membership/validating-user-credentials-against-the-membership-user-store-cs\">example如何验证凭据。然而,它使用的asp:控制登录表单
I'm trying to follow this example on how to validate credentials. However, it uses asp: controls for the login form.
如果我是用HTML控件代替CSS样式可以应用,如:
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>
我如何访问用户名和放大器;密码背后类似code?
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;
}
什么是正确的语法,而不是 UserName.Text,Password.Text
?
推荐答案
添加 ID
和 RUNAT
服务器属性输入的标记(见下文)
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"/>
您还需要修改文本
到值
在code:
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;
}
您还可以添加一个HTML 复选框
为与rememberMe
You can also add a html checkbox
for RememberMe
<input id="RememberMe" type="checkbox" runat="server" value ="RememberMe"/>
现在可以通过调用检查检查状态 RememberMe.Checked
Now you can check the checked states by calling RememberMe.Checked
这篇关于如何访问HTML控件在code背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!