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

  • Home
  • SEARCH
  • 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 9164481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:41:38+00:00 2026-06-17T14:41:38+00:00

I’m having some issues with a usercontrol . I have been at it for

  • 0

I’m having some issues with a usercontrol. I have been at it for many hours, so have to ask for help now.

If I call the user control from the mark-up page like this:

<uc7:MenuMain2 ID="MenuMain2" runat="server" RootCategoryId="4" ImageHeight="40"
                        ImageWidth="260" />

it works fine.


I can also call it with:

Control cont = LoadControl(@"../usercontrols/MenuMain2.ascx");
ContentPlaceHolder1.Controls.Add(cont );

and that works fine too. But then I don’t get to set the properties like I need too.


But if I call it to a placeholder like the code in the code below, it’s not working. I’m getting the error I have posted at the bottom of the page.

Usercontrol code file – MenuMain.ascx.cs:

public partial class MenuMain2 : System.Web.UI.UserControl {

 public int ActiveCategoryId { get; set; }
 public int RootCategoryId { get; set; }
 public int articleID = 0;
 public int ImageWidth ;
 public int ImageHeight ;

  protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack)
            BindData();
    }

    private void BindData() {
        CategoriesService catSvc = new CategoriesService();
        if (ActiveCategoryId == 0) {
            if (articleID > 0) {
                M02ArticlesService artSvc = new M02ArticlesService();
                List<M02ArticlesEntity> artList = artSvc.FindById(articleID);
                if (artList.Count > 0)
                    ActiveCategoryId = artList.First().CategoryId;
            }
        }
        List<CategoriesEntity> catList = new List<CategoriesEntity>();
        catList = catSvc.GetPublishedByParentCategoryId(RootCategoryId);
        ViewState.Add("ActiveCategoryId", ActiveCategoryId);
        Repeater1.DataSource = catList;
        Repeater1.DataBind();
    }
}

Code-behind for the Masterpage:

        public HttpCookie authCookie;
        public FormsAuthenticationTicket ticket;
        private string name;
        private MenuMain2 mn;

        protected void Page_Load(object sender, EventArgs e)
        {
            mn = new MenuMain2 {RootCategoryId = 4,ImageHeight = 100, ImageWidth = 260}
            //Control cont = LoadControl(@"../usercontrols/MenuMain2.ascx");

            if (Request.Cookies[".ASPXAUTH"] != null)
            {
                authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                ticket = FormsAuthentication.Decrypt(authCookie.Value);
                name = ticket.Name;
            }

            if (name != null)
            {
                if (name.Equals("Jhon"))
                {
                    mn.RootCategoryId = 1;
                }
                if (name.Equals("Bob"))
                {
                    mn.RootCategoryId = 2;
                }
                if (name.Equals("Tom"))
                {
                    mn.RootCategoryId = 3;
                }
            }

            ContentPlaceHolder1.Controls.Add(mn);
            CategoriesEntity cat;
            int catId = Request.GetCategoryId();

            if (catId > 0)
            {
                cat = new CategoriesService().GetById(catId);
                if (cat != null && cat.RequireLogin && Request.Url.LocalPath != "/login.aspx")
                {
                    if (!view_M06PersonsCategoriesService.HasAccess(HttpContext.Current.User.Identity.Name, catId))
                    {
                        Response.Redirect(string.Format("~/login.aspx?ReturnUrl={0}&qs={1}&CatId={2}{3}",
                                                         Server.UrlEncode(Request.Url.LocalPath),
                                                         Server.UrlEncode(Request.Url.Query),
                                                         Request.QueryString["CatId"],
                                                         Request.QueryString["ArtId"].IsEmpty() ? String.Empty : "&ArtId=" + Request.QueryString["ArtId"]));
                    }
                }
            }

            if (!Page.IsPostBack)
            {
                litAdmin.Text = ConfigurationManager.AppSettings["WebName"].ToString();
                string url = Request.ServerVariables["URL"].ToString();
                if (url.ToLower().StartsWith("/default.aspx"))
                    GetPhotoalbum();
            }
        }
      }
    }

Error message – the line 42 in the app is this one — Repeater1.DataSource = catList; —

`Object reference not set to an instance of an object.`

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   No.Itl.Web.WebApplication.usercontrols.MenuMain2.BindData() in e:\WorkFolder\usercontrols\MenuMain2.ascx.cs:42
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +41
   System.Web.UI.Control.OnLoad(EventArgs e) +131
   System.Web.UI.Control.LoadRecursive() +65
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427
  • 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-17T14:41:40+00:00Added an answer on June 17, 2026 at 2:41 pm

    Try this:

    MenuMain2 mn = (MenuMain2)LoadControl(@"../usercontrols/MenuMain2.ascx");
    
    ....
    
    if (name != null)
    {
        if (name.Equals("Jhon"))
        {
            mn.RootCategoryId = 1;
        }
        if (name.Equals("Bob"))
        {
            mn.RootCategoryId = 2;
        }
        if (name.Equals("Tom"))
        {
            mn.RootCategoryId = 3;
        }
    }
    

    You are loading the UserControl into cont but defining a type of MainMenu2 in mn and adding that object which hasn’t been loaded.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I

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.