• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 836
  • Last Modified:

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

0
ShoshanaSchechter
Asked:
ShoshanaSchechter
  • 4
  • 3
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now