简体   繁体   English

ASP.NET 控件在代码隐藏中不可用

[英]ASP.NET Control not available in CodeBehind

I drag and drop a Label control on my ABC.ASPX page.It Compiles correctly.我在我的 ABC.ASPX 页面上拖放了一个 Label 控件。它编译正确。 But the control ( Label2 ) is not available in code-behind ABC.ASPX.cs for value assignment.但是控件 ( Label2 ) 在代码隐藏 ABC.ASPX.cs 中不可用以进行值分配。

What is the resolution?分辨率是多少?

<%@ Page language="c#" CodeBehind="ABC.aspx.cs" AutoEventWireup="false" %> 

 <asp:Label ID="Label2" runat="server" Font-Bold="True"></asp:Label>

You are probably missing an entry for this in your designer.cs file.您可能在 designer.cs 文件中遗漏了一个条目。 You can either add something like this to the file:您可以将类似这样的内容添加到文件中:

protected global::System.Web.UI.WebControls.Label Label2;

Or delete the designer.cs file, right click the parent aspx file and choose "Convert to Web Application".或者删除designer.cs文件,右击父aspx文件,选择“转换为Web应用程序”。 This should recreate the designer.cs file with all the appropriate entries.这应该使用所有适当的条目重新创建 designer.cs 文件。

You might need to add Inherits="XXX.ABC" in your Page Directive, where XXX is your root namespace.您可能需要在页面指令中添加Inherits="XXX.ABC" ,其中 XXX 是您的根命名空间。 Also, I'm assuming that you named your class ABC to coincide with ABC.aspx.cs.此外,我假设您将 class ABC 命名为与 ABC.aspx.cs 一致。

Try to remove the control, save the file, add the control again, and again save the file.尝试删除控件,保存文件,再次添加控件,然后再次保存文件。 Problem is probably in your *.designer.cs file, this approach should regenerate it.问题可能出在您的 *.designer.cs 文件中,这种方法应该会重新生成它。

After creating a user component I wanted to test it in a CalendarTest page.创建用户组件后,我想在 CalendarTest 页面中测试它。 I too could not access the user component through the Code Behind.我也无法通过代码隐藏访问用户组件。 I did not find a solution on-line, but through trial and error I fixed it with the following steps:我没有在网上找到解决方案,但通过反复试验,我通过以下步骤修复了它:

1) in the aspx file, <%@Page... needed "CodeFile=" instead of "Src=" to point to the code behind file 1) 在 aspx 文件中,<%@Page... 需要“CodeFile=”而不是“Src=”来指向代码隐藏文件

2) in the Code behind aspx.cs file, I needed to replace "public class CalendarTest" with "public partial class CalendarTest" 2) 在 aspx.cs 文件背后的代码中,我需要将“public class CalendarTest”替换为“public partial class CalendarTest”

Right Click on the project then select convert to web Application右键单击项目,然后将 select 转换为 web Application

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

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