Visual Studio 2012新的ASP Web窗体应用程序无法从aspx文件访问文本框

时间:2013-07-26 19:41:21

标签: c# asp.net visual-studio-2012

我刚刚在VS 2012中创建了一个新的Web窗体应用程序,我正在尝试获取位于Login.aspx页面中的“UserName”文本框的值,所有我得到的是“名称UserName不存在于当前环境“当试图在login.aspx.cs文件中访问它时...

ASPX文件:

    <%@ Page Title="Log in" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="WebApplication1.Account.Login" %>
<%@ Register Src="~/Account/OpenAuthProviders.ascx" TagPrefix="uc" TagName="OpenAuthProviders" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <hgroup class="title">
        <h1><%: Title %>.</h1>
    </hgroup>
    <section id="loginForm">
        <h2>Use a local account to log in.</h2>
        <asp:Login runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
            <LayoutTemplate>
                <p class="validation-summary-errors">
                    <asp:Literal runat="server" ID="FailureText" />
                </p>
                <fieldset>
                    <legend>Log in Form</legend>
                    <ol>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
                            <asp:TextBox runat="server" ID="UserName" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                        </li>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                            <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                        </li>
                        <li>
                            <asp:CheckBox runat="server" ID="RememberMe" />
                            <asp:Label runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
                        </li>
                    </ol>
                    <asp:Button runat="server" CommandName="Login" Text="Log in" />
                </fieldset>
            </LayoutTemplate>
        </asp:Login>
        <p>
            <asp:HyperLink runat="server" ID="RegisterHyperLink" ViewStateMode="Disabled">Register</asp:HyperLink>
            if you don't have an account.
        </p>
    </section>

    <section id="socialLoginForm">
        <h2>Use another service to log in.</h2>
        <uc:OpenAuthProviders runat="server" ID="OpenAuthLogin" />
    </section>
</asp:Content>

CS文件:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = UserName.Text;
        }
    }
}

编辑: 添加了自动生成的aspx.designer.cs文件:

    //------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace WebApplication1.Account
{


    public partial class Login
    {

        /// <summary>
        /// RegisterHyperLink control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.HyperLink RegisterHyperLink;

        /// <summary>
        /// OpenAuthLogin control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::WebApplication1.Account.OpenAuthProviders OpenAuthLogin;
    }
}

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

首先,您应该为Login控件命名(例如Login1)。 然后引用它:

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Login1.UserName;
    }

答案 1 :(得分:0)

因为您使用LoginControl。试试这个:

TextBox tb = (TextBox)Login1.FindControl("UserName");

答案 2 :(得分:0)

(根据OP的要求将评论转换为答案)

啊,我看到UserName包含在<LayoutTemplate>中。由设计师创造的混乱控制。如果您将文本框移到外面,那么它就会起作用,但是如果您使用的是<asp:Login>(我建议不要这样做),那么您无需直接从代码隐藏中访问控件。