Reputation: 2981
<%@ 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>
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.
Upvotes: 0
Views: 3690
Reputation: 9166
The System.Web.UI.WebControls.Login
(asp:Login) control derives from System.Web.UI.WebControls.CompositeControl
which in turn implements INamingContainer
.
When a class implements INamingContainer, it becomes responsible for "keeping track" of it's child controls.
For these types of controls, the child controls cannot be accesses by simply referring to their ID in the code behind, because the child controls will be dynamically created by the parent control. Instead you have to use the parent control's FindControl
method to get a reference to them.
See Kane's answers about how to use FindControl.
Upvotes: 0
Reputation: 16802
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;
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;
}
}
Upvotes: 1
Reputation: 21355
I guess you are using a WebApplication instead of a website, if that's the case, you need to declare the controls in code, this is automatically done for you when you use Visual Studio to generate the ASPX page and the controls are declared as soon as you create a server control with the property ID
, the controls are registered in the designer file:
Something like this:
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;
}
}
When you are using a Website, you do not need to declare them, explicitly, that job will be done by the ASP.Net compiler the first time a user access your website
These files are created automatically by Visual Studio, you will see something like this in your solution explorer:
Upvotes: 1