Base Page and User Control Classes VS2008

I have built a web user control (.ascx) in a web application.
In a C# class library, I referenced the control library, and created code for the base page which is supposed to load the ascx control onto any page that derives from it.
In my web application, I referenced the c# class library that contains the base page. Then I derived all the web pages from my base class.
The pages run fine with no errors, but the control doesn't show
Base Page Code:
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace PensionCalculatorControlClass
{
    public class PensionCalculatorBasePage : System.Web.UI.Page
    {
        public PensionCalculatorBasePage()
        {
            this.Load += new EventHandler(OnPageLoad);
        }
 
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
 
        private menuControl.menuButtons menuObj;
 
        protected void OnPageLoad(object sender, EventArgs e)
        {
            menuObj = new menuControl.menuButtons();
            this.FindControl("tdMenu").Controls.Add(menuObj);
            menuObj.ID = "menuButtons";
            this.menuObj.PageSelected += new menuControl.menuButtons.PageSelectedEventHandler(menuButtons_PageSelected);
        }
 
        void menuButtons_PageSelected(object sender, PageSelectedEventArgs e)
        {
            Response.Redirect(e.pageName);
        }
    
    }
}

Open in new window

ShoshanaSchechterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan_BarreraCommented:
Hi,
Are you adding the control to page page directly through the page markup or dynamically in code-behind?
0
ShoshanaSchechterAuthor Commented:
dynamically. In the base page code I wrote:
this.FindControl("tdMenu").Controls.Add(menuObj);
OR
this.Controls.Add(menuObj);
OR
Page.Controls.Add(menuObj);

None of them work.
0
Juan_BarreraCommented:
Oh, sorry, I missed that part!
You can try this:
1: In the base class, create a property to store the parent control (tdmenu):
 
  private WebControl parentCtl;
       public WebControl ParentControl
    {
        get { return parentCtl  ; }
        set { parentCtl  = value; }
    }
 
2: In the inherited page, set the property:
 
ParentControl = The Control you want to add the User Control to (tdMenu)
 
 
3: Then, load and add the control like this, in Page Load of you base class.
 
Control uc = LoadControl("pathtofile");
parentControl.Controls.Add(uc);

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

ShoshanaSchechterAuthor Commented:
ok. that solves the problem of the control not being created on the page.
But how do I access the Control's properties and methods now?
In my former code I wrote:
        menuObj = new menuControl.menuButtons();
        menuObj.ID = "menuButtons";
        this.FindControl("tdMenu").Controls.Add(menuObj);
        this.menuObj.PageSelected += new menuControl.menuButtons.PageSelectedEventHandler(menuButtons_PageSelected);

Now I have:
        Control uc = LoadControl("UserControls/menuButtons.ascx");
        ParentControl.Controls.Add(uc);

How do I translate the line:
this.menuObj.PageSelected += new menuControl.menuButtons.PageSelectedEventHandler(menuButtons_PageSelected);
to work with "uc" ?
0
Juan_BarreraCommented:
If you want to do that you need to register the control with the inherited page, and add it differently. I would go for a CustomControl rather than a UserControl:

// This goes in the markup:
 
<%@ Reference Control="pathtofile" %>
 
// This goes in code behind:
menuControl.menuButtons() uc = (menuControl.menuButtons())LoadControl("pathtofile",menuControl.menuButtons());
ParentControl.Controls.Add(uc);    

Open in new window

0
ShoshanaSchechterAuthor Commented:
I'm not sure I know the difference between a custom control and a user control. I'll have to read up on that and see how I change the control.
0
Juan_BarreraCommented:
This would be a good place to start: http://support.microsoft.com/kb/893667
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.