Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:43:52+00:00 2026-06-09T11:43:52+00:00

Getting the good old Object reference not set to an instance of an object

  • 0

Getting the good old “Object reference not set to an instance of an object” error. Shortened everything down for the sake of abbreviation:

The .ascx file:

<asp:Label ID="lblcategory" runat="server" Text="Label"></asp:Label>

The .ascx.cs file:

public partial class NewsArticleContainer : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }      

    public NewsArticleContainer()
    {
        lblcategory.Text = "hello there!"; //null reference exception
    }
}

Then, I’m putting this User Control into another page after registering it with the web.config file.

The .aspx file:

<MyUC:NewsArticleContainer runat="server"/>

If I put the lblcategory.Text = "hello there!" line in Page_Load and comment it out of the constructor, it works just fine. However, later, I would like to be able to add instances of this User Control programmatically (i.e. mypanel.Controls.Add(new NewsArticleContainer(x)), where x is a NewsArticle).

I’m already aware of the requirement to use LoadControl to do this with a User Control — that’s not the problem. The problem is I can’t access the web controls in the User Control from the User Control’s constructor.

It just occurred to me while writing this: in the case of a User Control, could it be that Page_Load is the equivalent of a constructor in the traditional meaning of creating an instance of a class, and the traditional constructor public ClassName() of a UserControl shouldn’t be touched or modified?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:43:53+00:00Added an answer on June 9, 2026 at 11:43 am

    Well you need to understand some thing here. Though UserControl is a class, but control has its own life cycle. The individual control are only available for use once Init of the page life cycle/control life cycle has been called. Before Init, none of your controls are initalised as they do not have unique id’s assigned to them. That’s why you are getting object reference error. What you could do here is as follwing

    public partial class NewsArticleContainer : System.Web.UI.UserControl
    {
       List<string> NewsArticle = null;
       public NewsArticleContainer(List<string> toCreateNewsArticle)
       {
          NewsArticle = toCreateNewsArticle;
       }
    
        protected void Page_Load(object sender, EventArgs e)
        {
           foreach(string s in NewsArticle)
           {
              //dynamically create your label control and add it to this user control
              Label lb = new Label;
              lb.Text = s;
              this.Controls.Add(lb);
           }
        }
    }
    

    Pass the input and store the NewsArticle in your class level variable. Then in page load or page_init, dynamically create your label and add to your control

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading Getting Good With Git, I've learnt how to use Git, but not
I am trying to used following code but I am not getting good performance
I've spent a good while getting my AVR development system set up with the
Good Day Everyone... I’m getting an unexpected WCF error complaining of Known Types which
I've been doing some cocoa since a week, and I'm getting kinda good with
Good afternoon, I'm having serious troubles getting my database back up and running. I
Spent a good few hours on this now and don't seem to be getting
I understand that it is good practice to use a using block when getting
I'm getting back to programming for Google App Engine and I've found, in old,
I am getting this error with GT.M: %GTM-E-GDINVALID, Unrecognized Global Directory file format: /home/blah/gt.m/example/mumps.gld,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.