Search code examples
c#asp.netcode-behind

TextBox in .aspx is not accessible in .cs


I have tried a lot and spent my whole day on it but all in vain. I found number of questions related to my problem but no one is resolving my problem. I have a textbox in Login.aspx with ID="UserName" and trying to get its value in Login.aspx.cs but it gives error that "UserName doesn't exist in current context". here is my Login.aspx (created by default by VS 2012)

<%@ Page Title="Log in" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Login.aspx.cs" Inherits="cpool2.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" runat="server">
    <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:TextBox>
                        <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>
                        <%--<asp:HyperLink runat="server" ID="RegisterHyperLink" NavigateUrl="~/Account/ForgotPassword.aspx" ViewStateMode="Disabled">Forgot Password?</asp:HyperLink>--%>
                    </li>
                </ol>
                <asp:Button runat="server" CommandName="Login" Text="Log in" OnClick="btnLogin" />
            </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>
</asp:Content>  

Login.aspx.cs is as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Text;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration; 

namespace cpool2.Account
{
public partial class Login : System.Web.UI.Page
{
    //protected TextBox UserName;
    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterHyperLink.NavigateUrl = "Register.aspx";
        OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
        var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        if (!String.IsNullOrEmpty(returnUrl))
        {
            RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
        }   
    }
    protected void btnLogin(object sender, EventArgs e)
    {
        string abc = UserName.Text; // causing error
    }
  }
}

I am working in Web Application (asp.net framework 4.5). It may be my silly mistake but it made my day difficult for me :(


Solution

  • try this:

    TextBox UserName = (TextBox)Login1.FindControl("UserName");
    if (UserName != null)
    {
        string abc = UserName.Text;
    }