Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASPNET WebForms Server Control is NULL in Page_Load

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

like image 576
Svein Terje Gaup Avatar asked Nov 09 '12 09:11

Svein Terje Gaup


2 Answers

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

like image 194
Svein Terje Gaup Avatar answered Oct 14 '22 23:10

Svein Terje Gaup


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.

like image 28
Krishan Kumar Gorav Avatar answered Oct 14 '22 22:10

Krishan Kumar Gorav