ShoshanaSchechter
asked on
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
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);
}
}
}
ASKER
dynamically. In the base page code I wrote:
this.FindControl("tdMenu") .Controls. Add(menuOb j);
OR
this.Controls.Add(menuObj) ;
OR
Page.Controls.Add(menuObj) ;
None of them work.
this.FindControl("tdMenu")
OR
this.Controls.Add(menuObj)
OR
Page.Controls.Add(menuObj)
None of them work.
Oh, sorry, I missed that part!
You can try this:
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);
ASKER
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(menuOb j);
this.menuObj.PageSelected += new menuControl.menuButtons.Pa geSelected EventHandl er(menuBut tons_PageS elected);
Now I have:
Control uc = LoadControl("UserControls/ menuButton s.ascx");
ParentControl.Controls.Add (uc);
How do I translate the line:
this.menuObj.PageSelected += new menuControl.menuButtons.Pa geSelected EventHandl er(menuBut tons_PageS elected);
to work with "uc" ?
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")
this.menuObj.PageSelected += new menuControl.menuButtons.Pa
Now I have:
Control uc = LoadControl("UserControls/
ParentControl.Controls.Add
How do I translate the line:
this.menuObj.PageSelected += new menuControl.menuButtons.Pa
to work with "uc" ?
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);
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you adding the control to page page directly through the page markup or dynamically in code-behind?