点击事件无法识别asp.net中的文本框

时间:2013-09-12 20:48:48

标签: asp.net error-handling

我的点击事件显示错误"名称login_password和login_username在当前上下文中不存在"当我试图从他们的ID访问我的文本框。请帮我识别错误,因为我只是一个初学者。

点击事件:

protected void LogIn_Clicked(object sender, EventArgs e)
    {
        string usern = "abc";
        string pass = "123";

        if (login_username.Text == usern && login_password.Text == pass)
        {
            Response.Redirect("Default.aspx");

        }
        else
        {

               label1.Text = "LogIn";                            
        }

    }
}

登录表单

<form id="login" runat="server">
<h1 style = "Background-color: deepskyblue;">
    Log in to your account!</h1>
<p class="register">
    <a style = "color: deepskyblue;" href="Login.aspx?logoff=true">Logoff</a>
</p>
<div>
    <label for="login_username">
        Username</label>
    <input type="text" name="username" id="login_username" class="field required" title="Please provide your username" onclick="return login_username_onclick()" />
</div>
<div>
    <label for="login_password">
        Password</label>
    <input type="password" name="password" id="login_password" class="field required"
        title="Password is required" />
</div>
<div class="submit">
    <asp:Button ID="Button1" Text="Log in" runat="server" OnClick="LogIn_Clicked" 
        BackColor="#00B2EE" ForeColor="White" />

2 个答案:

答案 0 :(得分:1)

那些不是Asp.net控件。添加runat="server"属性:

<input type="text" name="username" id="login_username" class="field required" title="Please provide your username" onclick="return login_username_onclick()" runat="server"/>

或者您可以添加Asp.net控件:

<asp:TextBox runat="server" ID="login_username" CssClass="field required" ToolTip="Please provide your username"></asp:TextBox>

答案 1 :(得分:0)

您输入的用户名和密码仅为html - 他们需要一个runat =“server”标签才能将其置于后面的代码中。通常的方法是使用asp:Textbox。