18
votes

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! :)

3
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
Databinding is my desperate attempt at fixing the problem :S Will add the stack trace. - Svein Terje Gaup
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
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
Please set this question to answered if your problem is solved - Kees de Wit

3 Answers

18
votes

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! :)

1
votes

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
votes

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.