【问题标题】:web controls are not recognizing in the respective codebehind classWeb 控件无法在相应的代码隐藏类中识别
【发布时间】:2012-06-29 15:24:38
【问题描述】:
<%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="EQ.Account.Login" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
    Log In
</h2>
<p>
    Please enter your Employee Code and password.
    If you don't have an account Please Contact your Manager.
</p>
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
    <LayoutTemplate>
        <span class="failureNotification">
            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
        </span>
        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
             ValidationGroup="LoginUserValidationGroup"/>
        <div class="accountInfo">
            <fieldset class="login">
                <legend>Account Information</legend>
                <p>
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                    <asp:TextBox ID="txtUserName" runat="server" CssClass="textEntry"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                         CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p>
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                    <asp:TextBox ID="txtPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                         CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p>
                    <asp:CheckBox ID="RememberMe" runat="server"/>
                    <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
                </p>
                <p>
                <asp:Label ID="lblErrorMessage" runat="server" CssClass="failureNotification" Visible="false" />
                </p>
            </fieldset>
            <p class="submitButton">
                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup" OnClick="LoginButton_Click"/>
            </p>
        </div>
    </LayoutTemplate>
</asp:Login>

这是我的 ASP 页面,但在 CodeBehind 页面(即 Login.aspx.cs)中,当我尝试获取 lblErrorMessage、txtuserName 和 txtPassword 等控件的值时,我收到“错误 1 ​​名称 'txtUserName' 的错误”当前上下文中不存在 C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Account\Login.aspx.cs 64 46 EQ ”。 等待是否有人会解决这个问题并指导我为什么它无法识别背后代码中的控件。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    要访问UserName 文本框,您将使用 FindControl 方法遍历对象树。例如,

    string myValue = (LoginUser.FindControl("UserName") as TextBox).Text;
    

    为了让生活更轻松一些并且代码更具可读性,您可以添加一个扩展方法来返回强类型控件。

    // usage
    var myControlUsingExtensionMethod = LoginUser.FindControl<TextBox>("UserName").Text;
    
    public static class ControlExtensions
    {
        public static T FindControl<T>(this Control control, string id) where T : Control
        {
            return control.FindControl(id) as T;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我猜您使用的是 WebApplication 而不是网站,如果是这种情况,您需要在代码中声明控件,当您使用 Visual Studio 生成 ASPX 页面并声明控件时,这会自动为您完成一旦您使用属性ID 创建了一个服务器控件,这些控件就会在设计器文件中注册:

      类似这样的:

      namespace WebApplication1 {
      
      
          public partial class DynamicControls {
      
              /// <summary>
              /// form1 control.
              /// </summary>
              /// <remarks>
              /// Auto-generated field.
              /// To modify move field declaration from designer file to code-behind file.
              /// </remarks>
              protected global::System.Web.UI.HtmlControls.HtmlForm form1;
      
              /// <summary>
              /// lblMessage 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.Label lblMessage;
          }
      }
      

      当您使用网站时,您无需明确声明它们,该工作将在用户第一次访问您的网站时由 ASP.Net 编译器完成

      这些文件是由 Visual Studio 自动创建的,您会在解决方案资源管理器中看到类似这样的内容:

      【讨论】:

        【解决方案3】:

        System.Web.UI.WebControls.Login (asp:Login) 控件派生自System.Web.UI.WebControls.CompositeControl,后者又实现INamingContainer

        当一个类实现 INamingContainer 时,它负责“跟踪”它的子控件。

        对于这些类型的控件,子控件在后面的代码中是不能通过简单的ID来访问的,因为子控件是由父控件动态创建的。相反,您必须使用父控件的 FindControl 方法来获取对它们的引用。

        查看 Kane 关于如何使用 FindControl 的答案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多