名称xxx在特定上下文中不存在

时间:2013-03-23 00:21:51

标签: asp.net

为什么每当我尝试调试代码时,我都会在VS中收到以下错误消息?

Error   2   The name 'Password' does not exist in the current context   Login.aspx.cs   22  22  Team13
Error   1   The name 'UserName' does not exist in the current context   Login.aspx.cs   21  27  Team13

这是我的Login.aspx代码

 <form id="loginForm" runat="server">
    <div id="login_box" runat="server">
        <!-- LOGIN BOX -->
            <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate" Width="293px"
                Height="172px">
                <LayoutTemplate>
                    User Name:<br />
                    <asp:DropDownList ID="UserName" runat="server" DataSourceID="SqlDataSource1" DataTextField="dept"
                        DataValueField="id" Width="200px">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:team13ConnectionString %>"
                        SelectCommand="SELECT [id],[dept] FROM [ts_dept] ORDER BY [id]"></asp:SqlDataSource>
                    <br /><br />
                    Password:<br />
                    <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                        ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                    <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                    <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                    <a href="http://89.200.143.163/~team13/login.php">Switch to PMW</a>
                    <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" />
                </LayoutTemplate>
            </asp:Login>
        </div>
    </form>

这是我的Login.aspx.cs代码

using System;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;

public partial class login : System.Web.UI.Page
{

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string username = Login1.UserName;
        string pwd = Login1.Password;

        string strConn;
        strConn = WebConfigurationManager.ConnectionStrings["team13ConnectionString"].ConnectionString;
        SqlConnection Conn = new SqlConnection(strConn);
        Conn.Open();

        string sqlUserName;
        sqlUserName = "SELECT id,pass FROM ts_dept ";
        sqlUserName += " WHERE (id ='" + username + "')";
        sqlUserName += " AND (pass ='" + pwd + "')";
        SqlCommand com = new SqlCommand(sqlUserName, Conn);

        string CurrentName;
        CurrentName = (string)com.ExecuteScalar();

        if (CurrentName != null)
        {
            Session["UserAuthentication"] = username;
            Session.Timeout = 1;
            Response.Redirect("Default.aspx");
        }
        else
        {
            Session["UserAuthentication"] = "";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

1 个答案:

答案 0 :(得分:2)

这些控件位于Login自定义控件中。即使您在页面中“看到它们”,您也只能使用FindControl实际访问它们:

TextBox txtUserName = Login1.FindControl("UserName") as TextBox;
相关问题