C# 如何在后面的代码中访问html控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16875238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:01:03  来源:igfitidea点击:

How to access html controls in code behind

c#asp.net

提问by user3357963

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

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

If I were to use html controls instead so CSS styles can be applied, eg

如果我改用 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>

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?

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

采纳答案by Damith

Add idand runatserver attributes to the input tag (see below)

添加idrunat服务器属性到输入标签(见下文)

<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 Textto Valuein your code:

您还需要在代码中更改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;
}

You can also add a html checkboxfor RememberMe

您还可checkbox以为RememberMe

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

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

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

回答by cheedep

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

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

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

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

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

回答by Matt Varblow

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

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

Then you can access the values like this:

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

Membership.ValidateUser(UserName.Value, Password.Value)