Web控件没有在各自的codebehind类识别 [英] web controls are not recognizing in the respective codebehind class

查看:115
本文介绍了Web控件没有在各自的codebehind类识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%@ 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:\\用户\\ dsingh \\文档\\ Visual Studio 2010的\\项目\\ EQ \\ EQ \\帐户\\ Login.aspx.cs 64 46 EQ

等待,如果一些人会解决这个问题,并指导我,为什么不从背后的code识别控制。

This is my ASP page but in the CodeBehind page i.e. Login.aspx.cs, when i m trying to get the values of the controls like lblErrorMessage,txtuserName and txtPassword , i m getting error that "Error 1 The name 'txtUserName' does not exist in the current context C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Account\Login.aspx.cs 64 46 EQ ". Waiting if some one will solve this problem and guide me that why it is not recognizing the controls from the code behind.

推荐答案

要访问用户名文本框,你将有遍历使用FindControl方法的对象树。如,

To access the UserName text box you will have traverse the object tree using the FindControl method. E.G.,

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

为了让生活更容易一点和code更具可读性,您可以添加一个扩展方法返回强类型的控件。

To make life a bit easier and the code more readable you can add an extension method to return strongly typed controls.

// 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;
    }
}

这篇关于Web控件没有在各自的codebehind类识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆