繁体   English   中英

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

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

我在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>

当我尝试访问文本框时,在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.
    }
}

找不到文本框!!!

  • 这个错误的原因是什么?
  • 我该如何解决?

我认为您应该使用var txt1 = Content1.FindControl("TextBox1")然后如果txt1不为空,请像通常使用TextBox1一样使用它?

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

在声明中将CodeFile更改为Codebehind

更新检查 InfoEdition.aspx 页面的“构建操作”属性设置为? 它应该设置为“内容”。

我的回答有点晚了,但您可以做的一件事是检查设计器文件是否存在于您的 ASPX 页面所在的文件夹中。

如果缺少设计器文件,代码隐藏将无法找到您的字段。

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

暂无
暂无

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

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