19

I have a very strange situation where my Label control is NULL in Page_Load. Should it even be possible?

I have a Web Form:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyNamespace.Templates.Pages.WebForm1" %>
<%@ Register TagPrefix="cw" Namespace="MyNamespace.Templates.Units" Assembly="MyNamespace" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="background-color:yellowgreen;">
        FIRST
    </div>
    <div style="background-color: wheat;">
        SECOND
        <cw:ArticleColumn runat="server" ID="ac" ArticleID="1899"/>
    </div>
    </form>
</body>
</html>

In the Page_Load of the web form, I simply do a

if(!IsPostBack) DataBind();

The User control ArticleColumn looks like this:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ArticleColumn.ascx.cs"  Inherits="MyNamespace.Templates.Units.ArticleColumn" %>
<h2><asp:Label runat="server" ID="myArticleHeading" Text="TOM" /></h2>
<p><asp:Label runat="server" ID="myArticleIntro" Text="TOM" /></p>
<asp:Label runat="server" ID="myArticleBody" Text="TOM" />

And the code behind looks like this:

public partial class ArticleColumn : UserControlBase<PageTypes.ArticleBase>
{
    public int ArticleID { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        var page = DataFactory.Instance.GetPage(new PageReference(ArticleID));

        myArticleHeading.Text = page.PageName;
        myArticleIntro.Text = page.Property["IntroText"] != null
                            ? page.Property["IntroText"].ToWebString()
                            : string.Empty;
        myArticleBody.Text = page.Property["MainBody"] != null
                            ? page.Property["MainBody"].ToWebString()
                            : string.Empty;

        DataBind();
    }
}

It fails when I try to set myArticleHeading.Text because the control is null. How is this even possible? The ASP.NET automatic binding of controls must have somehow failed...

I have tried: - Restarting VS 2010 - Switching build configurations - Cleaning/Rebuilding - Changing names of controls so that the designer file is updated.

Needless to say, none of the above worked.

Edit: added stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   MyNamespace.Templates.Units.ArticleColumn.Page_Load(Object sender, EventArgs e) in C:\DevProjects\CustomerWeb\Main\Source\MyNamespace\Templates\Units\ArticleColumn.ascx.cs:21
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Control.LoadRecursive() +146
   System.Web.UI.Control.LoadRecursive() +146
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Solution: I just found the answer myself. Using this declaration for the usercontrol on the page fixed the problem:

<%@ Register TagPrefix="cw" TagName="ArticleColumn" Src="~/Templates/Units/ArticleColumn.ascx" %>

This faulty declaration was added using ReSharper right click menu from html view:

<%@ Register TagPrefix="cw" Namespace="MyNamespace.Templates.Units" Assembly="MyNamespace" %>

Thanks for the help! :)

Svein Terje Gaup
  • 1,424
  • 15
  • 29
  • Why are you making a generic call to DataBind both on the web form's page load and the user control's page load? In fact, why are you calling databind at all? Also, can you please include the exception / stack trace? – Lawrence Johnson Nov 09 '12 at 09:31
  • Databinding is my desperate attempt at fixing the problem :S Will add the stack trace. – Svein Terje Gaup Nov 09 '12 at 09:41
  • K, for one. Get rid of those DataBind() calls. They aren't doing anything. Second, before the myArticleHeading.Text = page.PageName; line, add this: string sTest = page.PageName; the goal is to confirm that its not the 'page' variable that is null. – Lawrence Johnson Nov 09 '12 at 09:57
  • Th solution was to declare the control in stead of declaring the namespace (see the question). ReSharper did a bad job adding references. – Svein Terje Gaup Nov 09 '12 at 11:23
  • 1
    Please set this question to answered if your problem is solved – Kees Jul 21 '13 at 18:11
  • @KeesdeWit It's impossible for 2 days I think or something like that. – Simon Dugré Sep 12 '13 at 20:48

3 Answers3

19

I answered my own question above.

Solution: I just found the answer myself. Using this declaration for the usercontrol on the page fixed the problem:

<%@ Register TagPrefix="cw" TagName="ArticleColumn" Src="~/Templates/Units/ArticleColumn.ascx" %>

This faulty declaration was added using ReSharper right click menu from html view:

<%@ Register TagPrefix="cw" Namespace="MyNamespace.Templates.Units" Assembly="MyNamespace" %>

Thanks for the help! :)

Svein Terje Gaup
  • 1,424
  • 15
  • 29
  • 1
    Just an explanations: you shold load conrol by path, not by assembly and namespace. Be careful with it. – Alex Zhukovskiy May 25 '15 at 12:54
  • I found that I had to register the control in the `web.config` file instead for it to work – drzaus Nov 23 '15 at 19:46
  • 2
    Thank you! I have spent an hour while trying to figure out the cause of my problem, and I would have spent more if not this answer :) – Yeldar Kurmangaliyev Feb 21 '17 at 07:59
  • Note to my future self: Check whether Visual Studio Code formatted declarations on the top of the file. – Slaven Semper Apr 24 '19 at 10:22
  • 1
    I have spent an entire morning trying to understand what was happening. It was ReSharper too, that imported the reference as a namespace inside the ascx as well. Thank you! – Fabricio Nov 23 '20 at 01:13
1

DataBind is something for binding the controls written in aspx part with server side script ie <% %>.

But your scenario does not require to call databind(). Just assigning the values to controls should work fine.

Try by removing databind from your code.

0

Update the content of the user control from the page.

Declare properties in the user control to update the labels content and use those properties from the page with the user control instance.

Juan
  • 1,352
  • 13
  • 20