简体   繁体   English

在 aspx 页面后面的代码中找不到控件

[英]Control not found in the code behind of an aspx page

I have this code in InfoEdition.aspx :我在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 :当我尝试访问文本框时,在InfoEdition.aspx.cs后面的代码中:

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?我该如何解决?

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不为空,请像通常使用TextBox1一样使用它?

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

Change CodeFile to Codebehind in the declarations.在声明中将CodeFile更改为Codebehind

UPDATED Check the "Build Action" property of the InfoEdition.aspx page set to?更新检查 InfoEdition.aspx 页面的“构建操作”属性设置为? It should be set to "Content".它应该设置为“内容”。

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.我的回答有点晚了,但您可以做的一件事是检查设计器文件是否存在于您的 ASPX 页面所在的文件夹中。

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为了重新创建设计器字段,您需要使用VS2019 Project --> Convert to Web Application 菜单选项重建它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM