Search code examples
c#asp.net.netmaster-pagescode-behind

Control not found in the code behind of an aspx page


I have this code in InfoEdition.aspx :

<%@ Page Title="" Language="C#" MasterPageFile="~/Espace_Candidat/SousCandidat.master" AutoEventWireup="true" CodeFile="InfoEdition.aspx.cs" Inherits="Espace_Candidat_InfoEdition" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ChildContent2" Runat="Server">
<div class="span9">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
 </div>
</asp:Content>

In the code behind InfoEdition.aspx.cs when i try to access to the textbox :

public partial class Espace_Candidat_InfoEdition : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
// THE TEXTBOX IS NOT FOUND
      TextBox1.
    }
}

the textbox is not found!!!

  • What are the reasons of this error?
  • How can i fix it?

Solution

  • I think you should be using var txt1 = Content1.FindControl("TextBox1") and then if the txt1 is not null use it as you would normally use TextBox1 ?

    var txt1 = Content1.FindControl("TextBox1");
    txt1.Text = "some value";