Lamloumi Afif
Lamloumi Afif

Reputation: 9081

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!!!

Upvotes: 1

Views: 2753

Answers (3)

abhi
abhi

Reputation: 3136

My answer is a bit late, but one of the things you could do is the check if the designer file exists in the folder where your ASPX page is.

If the designer file is missing, code-behind cannot find your fields.

In order to recreate the designer field, you'll need to rebuild it using the VS2019 Project --> Convert to Web Application menu option

Upvotes: 0

FrankO
FrankO

Reputation: 2562

Change CodeFile to Codebehind in the declarations.

UPDATED Check the "Build Action" property of the InfoEdition.aspx page set to? It should be set to "Content".

Upvotes: 1

Mauro
Mauro

Reputation: 4511

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";

Upvotes: 2

Related Questions