ASP / C # code behind cannot achieve markup control

Question:
I have this markup (only important lines):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RTDeluxe.ascx.cs" Inherits="MainSolution.CONTROLTEMPLATES.Kunde.RTDeluxe" %> <ul id="linkUl" class="teaserLinksUL" runat="server"/> 

The code:

 namespace MainSolution.CONTROLTEMPLATES.Kunde public partial class RTDeluxe : UserControl { protected void Page_Load(object sender, EventArgs e) { linkUl.InnerHtml = string.Empty; } } 

I can access ul inside the code and get a compilation error. But, when I debug the code, I get a NullReferenceException because linkUl is NULL.

At first, I thought that namespaces are the cause. But, after several attempts, I am sure that they are correct. FileLocation seems correct, and in the controltemplates folder of my iis there is a "Kunde" folder with the corresponding ascx files.

I have other .ascx files with the same structure -> they work like a charm.

Question:
Are there any other reasons besides the namespace for this behavior? Do you have any clues I can look at?

Edit:
The RTDeluxe.ascx.designer.cs file exists, the generated linkUl looks like this:

 protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl; 

Edit2:
Well, I will try to answer all your questions. Thanks for your time and feedback!

  • I restarted Visual Studio -> The problem persists.
  • I also cleared the solution and deployed a new one. → The problem persists.
  • When I debug and check the management hierarchy, I see that the label does NOT exist.
  • When I change the identifier, the compiler throws an error in the code (this is correct). If I change the identifier, I get the same behavior as before.
  • I also restarted my IIS and the whole PC -> No change.
  • I added the Name attribute to the definition of linkul -> No change.
  • When I try to use FindControl , it returns NULL.
  • Target Structure - .NET 3.5
  • Linkul is not inside the repeater or any other controls.
  • Removing / modifying web.config also does not lead to a solution.
  • Adding EnsureChildControls before accessing linkUl will not change anything.
  • Moving code in Page_PreRender also does not work.

I will try your suggestions not listed here and add them soon.

Edit3:
Here's the complete markup:

 <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RTDeluxe.ascx.cs" Inherits="MainSolution.CONTROLTEMPLATES.Kunde.RTDeluxe" %> <ul id="linkUl" class="teaserLinksUL" runat="server"/> 

* Edit4:
Ok, here are some additional information that I learned: When I change something in the markup, for example, by adding plain html text, it is NOT recognized or displayed in the browser. When I do something like this:

  Label label1 = new Label(); label1.Text = "hugo lives!"; Controls.Add(label1); 

Shown. It seems that everything is in order in the visual studio ... But "live" on the server, the code-code speaks of some strange different markup ...

+6
source share
5 answers

This may help you a bit:

In a code file under user control at the class level, add this code:

 protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl = new System.Web.UI.HtmlControls.HtmlGenericControl(); 

and delete the file protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl; from RTDeluxe.ascx.designer.cs

Perhaps this is due to the fact that its object was simply not created. Hope this helps.

+1
source

Have you tried calling EnsureChildControls before accessing the control or moving code to OnPreRender ?

0
source

I had a problem similar to the previous one in the .aspx file. This was because optimizeCompilations was set to true in my web.config file.

 <system.web> <compilation optimizeCompilations="true" /> </system.web> 

If optimizeCompilations is set to true, ASP.Net restores pages only when it seems necessary. Sometimes it gets confused and does not understand that you made changes that require re-sorting the page, which leads to a runtime error that the compiler does not detect. See http://msdn.microsoft.com/en-us/library/ms366723.aspx for more information on this option.

To fix the problem, I had to temporarily set optimizeCompilations to false, rebuild my site, recycle my application pool and reinstall it again.

Hope this helps.

0
source

Sometimes in such situations, it may happen that Visual Studio gets into a bad state with respect to this file. I found that deleting files and re-creating them often solve the problem. Be sure to copy the code somewhere so that you can paste it back into the newly created files.

0
source

Are you deployed using the publishing feature in Visual Studio? If so, try deleting the .ascx file on the destination server. I had a visual studio not recognizing that the file was modified and then not copying the new file.

0
source

Source: https://habr.com/ru/post/905521/


All Articles