6

Issue:
I have a markup like this (only the 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-behind:

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

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

First I thought that the namespaces are the reason. But, after several tries, I'm sure that they're correct. The FileLocation seems to be correct and the controltemplates folder of my iis has a "Kunde" folder with the corresponding ascx files in it.

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

Question:
Are there any other reasons than the namespace for such behaviour? Do you have any hints where 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:
Ok, I will try to answer all your questions. Thanks for your time and feedback!

  1. I have restarted Visual Studio -> The Problem persists.
  2. I also have cleaned up the solution and deployed a new one. -> The problem persists.
  3. When I debug and check the control hierachy I can see that the label is NOT there.
  4. When I change the ID the compiler throws an error in the code-behind (which is right). If i change the ID there two I get the same behavoiur as before.
  5. I also restarted my IIS and the whole pc -> No changes.
  6. I have added a Name attribute to the linkul-definition -> No changes.
  7. When I try to use FindControl it returns NULL.
  8. The target-framework is .NET 3.5
  9. The linkul is NOT inside a repeater or any other controls.
  10. Removing/changing the web.config does also not lead to a solution.
  11. Adding EnsureChildControls before accessing the linkUl doesnt change anything.
  12. Moving the code into Page_PreRender does also not work.

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

Edit3:
Here the full 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 some additional info I found out: When I change something in the markup, like adding plain html text it's NOT recognized by or shown in the browser. When i do something like this:

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

It is shown. It seems like in visual studio everything is fine... But "live" at the server the code-behind speaks to some weird different markup...

LMW-HH
  • 1,193
  • 2
  • 15
  • 31
  • Can you show your compiler generated definition for linkUl? – Dave Walker Jan 09 '12 at 14:36
  • do you have the RTDeluxe.designer.cs file? – giammin Jan 09 '12 at 14:50
  • @rangitatanz do you mean the code in the designer.cs or the generated html-code from the page? – LMW-HH Jan 09 '12 at 15:08
  • If you debug and check your control hierachy, or use a recursive findcontrols to check if your control is there, ... is it there as something else? – Dave Walker Jan 09 '12 at 15:20
  • code seems ok. did you try to restart visualstudio and iis – giammin Jan 09 '12 at 15:35
  • That is crazy. The only solutions that come to mind are simple ones, but: does this problem persist if you change the control's ID? What if you add a name attribute? Have you tried (after backing it up) removing your web.config from the project to see if there's some problem with it? – jwheron Jan 09 '12 at 15:40
  • I've had problems similar to this before, but I was always able to solve it by cleaning the project (Build->Clean) and rebuilding it (Build->Rebuild). I assume you already tried that. – ean5533 Jan 09 '12 at 15:42
  • I'm sure code is ok.It seems that you have AutoEventWireup=false. Did you restart VS and IIS or deleted the bin folder? Which version of framework are you targeting? if you try with FindControl what happens? – giammin Jan 09 '12 at 15:48
  • 1
    @lmw is that control inside a repeater or similar databound controls? – giammin Jan 09 '12 at 15:55
  • @giammin thank you very much for your time! please have a look at the updated question. – LMW-HH Jan 09 '12 at 16:07
  • @jwiscarson thanks for your feedback! please have a look at the updated question – LMW-HH Jan 09 '12 at 16:14
  • i surrender... i don't know what else could it be – giammin Jan 09 '12 at 16:16
  • How are you adding your control to the Page? At what point in the lifecycle - or are you referencing it in the ASPX? – Dave Walker Jan 09 '12 at 16:25
  • Is the control defined within a
    server tag?
    – Dave Walker Jan 09 '12 at 16:27
  • @rangitatanz it's declared in the RTDeluxe.ascx, I access it in the RTDeluxe.ascx.cs (if that is what you want to know, im not 100% sure) – LMW-HH Jan 09 '12 at 16:28
  • @rangitatanz to your second question: yes – LMW-HH Jan 09 '12 at 16:29
  • @lmw could you post the stacktrace? – giammin Jan 09 '12 at 17:12
  • 2
    Yeah ok. ASCX is a server control - it needs to be defined within a page (ASPX). How do you do that? Usually you have a <%@ Register TagPrefix or else you use LoadControl("/path-to-control/control.ascx"); – Dave Walker Jan 09 '12 at 17:31
  • Are you perhaps instantiating this control with `x = new RTDeluxe()` instead of using `LoadControl`? If the controls are null, in every scenario I've seen, it's a partial class decalaration or instantiation problem: the class name in the ASCX file doesn't match codebehind, or you are trying to create an instance of it using `new` (which technically is legal, and I fault the compiler for not warning about, but it only knows about the codebehind). – Jamie Treworgy Jan 09 '12 at 20:35

5 Answers5

1

This might help You a bit:

On the code-behind file of the user-control at class level, add this code:

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

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

It might be because its Object was just declared not created. Hope this helps..

RohitWagh
  • 1,999
  • 3
  • 22
  • 43
0

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

s_hewitt
  • 4,252
  • 24
  • 24
  • thanks for the suggestions :) i tried both of them, unfortunately it doesnt change the behaviour :( – LMW-HH Jan 09 '12 at 16:23
  • Pretty sure all EnsureChildControls does is ensures that CreateChildControls method is only called once. Page doesnt override that by default and Control has an empty abstract method. – Dave Walker Jan 09 '12 at 16:24
0

I've had a problem similar to this before in an .aspx file. It was caused by having optimizeCompilations set to true in my web.config file.

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

When optimizeCompilations is set to true ASP.Net only rebuilds pages when it feels it is necessary. Sometimes it gets confused and doesn't realize you've made a change that needs a page rebuild resulting in a run-time error that the compiler doesn't catch. See here for more information about this setting http://msdn.microsoft.com/en-us/library/ms366723.aspx.

To fix the problem I had to temporarily set optimizeCompilations to false, rebuild my website, recycle my app pool, and then set it back to true again.

Hope this helps.

Mark Rucker
  • 6,952
  • 4
  • 39
  • 65
0

Sometimes in situations such as this it may just be that Visual Studio has got into a bad state with regard to this file. I have found that deleting the files and recreating them will often resolve the issue. Make sure to copy the code somewhere so that you can paste it back into the newly created files.

0

Are you deploying using the visual studio publish functionality? If so, try deleting the .ascx file on your destination server. I have had visual studio not recognize that the file has changed and then doesn't copy the new file over.

sellandb
  • 346
  • 1
  • 7