简体   繁体   中英

ASP.NET Control not available in CodeBehind

I drag and drop a Label control on my ABC.ASPX page.It Compiles correctly. But the control ( Label2 ) is not available in code-behind ABC.ASPX.cs for value assignment.

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. 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". This should recreate the designer.cs file with all the appropriate entries.

You might need to add Inherits="XXX.ABC" in your Page Directive, where XXX is your root namespace. Also, I'm assuming that you named your class ABC to coincide with 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.

After creating a user component I wanted to test it in a CalendarTest page. 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

2) in the Code behind aspx.cs file, I needed to replace "public class CalendarTest" with "public partial class CalendarTest"

Right Click on the project then select convert to web Application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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