Cast ascx within App_Code class - asp.net

I have an ascx which I want to load and cast from a class sitting within App_Code. I can't get it to work from the App_Code class, although I can get it to work from an aspx page.
The ASPX page technique works fine with the following code:
pc = LoadControl("enquirycapture.ascx");
((ASP.enquirycapture_ascx)pc).CustomProperty = customObject;
(Note: I have the following in the aspx page:)
<%# Reference VirtualPath="~/enquirycapture.ascx" %>
However, when I try casting the control from within the App_Code class then it can't 'see' the ascx class, and therefore I am unable to cast to it to set the custom properties(I can load it, but not cast it). I don't know how to replicate the <% Reference...> thing from within the App_Code class. Anyone know how I can reference (and thus cast) my ascx from the App_Code class? Thanks.

App_Code compiles to a seperate assembly that can't reference types in a CodeFile.
But you can add interface/base class to your App_Code folder that identifies the custom properties and methods that you intend to implement in your usercontrol:
public class EnquiryCaptureBase : System.Web.UI.UserControl
{
public object CustomProperty { get; set; }
}
and then
public partial class EnquiryCapture : EnquiryCaptureBase
{
}
and finally somewhere in App_Code:
pc = LoadControl("enquirycapture.ascx");
((EnquiryCaptureBase)pc).CustomProperty = customObject;

Related

2 master pages one user control, how do I find what master page is being used?

So I have 2 master pages and one user control which is used on almost every page. Page may have one of 2 master pages and both master pages have one property common. It is a list variable that I need access. How do I know which master page is being used and then access it?
I am trying
MasterPage mp = (MasterPage)this.Page.Master;
but when I debug, I don't see the list property. mp.List doesn't work. Any idea on how to get this property?
Thanks in advance
Consider using a simple interface to make this easy.
public interface IHasProperty
{
List<string> MyVariable {get;set;}
}
public partial class MasterPage1 : (other stuff), IHasProperty
{
List<string> MyVariable {get;set;}
}
public partial class MasterPage2 : (other stuff), IHasProperty
{
List<string> MyVariable {get;set;}
}
then from the user control, you can access this by using something like this.
var myPropPage = Page.Master as IHasProperty;
if (myPropPage == null)
{
//this property isnt on the page.
return;
}
myPropPage.MyVariable.Add("new Value");// or whatever you needed to do with it.
Add a strongly typed reference to the master page on your aspx as so:
<%# MasterType VirtualPath="~/Site.Master" %>
Then in the code behind you can do this.Master without having to cast and the list should be accessible.

ASP.Net Calling Site.Master method from web page not working

I saw on several web pages how to interface to a public method defined in a master file from a web page call behind code that uses that master file.
(I am using ASP.Net 4.0 on Visual Studio 2012.)
The procedure is (copied from article):
Make sure the function is accessible to the page (i.e. declared
public), and use the MasterType declaration in the ContentPage:
<%# Page .... %>
<%# MasterType VirtualPath="~/masterpage.master" %>
In the page, use Page.Master.MyFunction() to access the function.
*Note: before being able to access the function, you'll need to save & build.
The problem is that I do not see the method. Here is what I have:
Web Page (stored in /MyFolder, so /MyFolder):
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Logout.aspx.cs" Inherits="BvCorpMain.Candidates.Logout" %>
<%# MasterType VirtualPath="/SiteMasters/Site.Master" %>
Site.Master CS file (stored in /SiteMasters folder):
public void UpdateUserBlocksToCookie()
{
}
When I go into the code behind for the logout page and in a method I type in "Page.Master.", I do not see my method.
Your page is inheriting from System.Web.UI.Page, which only knows that its master page is of type System.Web.UI.MasterPage. If you are making modifications to a child class of MasterPage, then you need to cast the Page.Master property to your child class.
public class MyPage : System.Web.UI.Page
{
public new MyMaster Master { get { return base.Master as MyMaster; } }
public void Page_Load(object sender, EventArgs e)
{
Master.MyMasterPageFunction();
}
}
public class MyMaster : System.Web.UI.MasterPage
{
public void MyMasterPageFunction()
{
}
}
The previous answer did educate me, however I believe the resolution was to restart VS2012, maybe cleaning the solution and rebuilding did not hurt. Either way.
Microsoft adds in the following code automatically to the .aspx.designer.cs file.
/// <summary>
/// Master property.
/// </summary>
/// <remarks>
/// Auto-generated property.
/// </remarks>
public new MyNamespace.Site Master {
get {
return ((BvCorpMain.Site)(base.Master));
}
The previous answer conflicts with this definition. Also, the previous answer of MyMaster, although granting access does not give (automatically at least) to needed form information. I checked. Using the existing master file is the cleanest.
The definition for the master.cs file is:
namespace MyNamespace
{
public partial class Site : System.Web.UI.MasterPage
As you can see, Microsoft did give access to MyNamespace.Site, which is what I needed, with "Master.".
I did not think to check the .aspx.designer.cs file for that definition, when I was having the problems. Possibly the definition was lacking and got added later, when either I rebuilt or did a save, which I had previously done, or whatever.
Knowing the addition does simplify things, as I can add that in manually if it does not exist using that construct.

Intellisense for Templated User Control Tags

I've written a templated user control, MinimalTemplate, which currently does nothing other than render the HTML passed into its "ContentTemplate" placeholder. I want Visual Studio 2008 to have the same intellisense features for MinimalTemplate that it has for built-in templated controls such as Repeater.
Possibly related: I can manually type out my ContentTemplate tags, and it will build and run properly, but I get a validation error. I have already deleted the contents of my ReflectedSchemas folder, as suggested in this question.
Complete source for Minimal Template:
MinimalTemplate.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MinimalTemplate.ascx.cs" Inherits="MyProject.MinimalTemplate" %>
<asp:placeholder runat=server id="contentPlaceHolder" />
MinimalTemplate.ascx.cs
using System.Web.UI;
namespace MyProject
{
[ParseChildren(false)]
public partial class MinimalTemplate : System.Web.UI.UserControl
{
[TemplateContainer(typeof(MessageContainer))]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate ContentTemplate
{ get; set; }
void Page_Init()
{
if (ContentTemplate != null)
{
MessageContainer container = new MessageContainer();
ContentTemplate.InstantiateIn(container);
contentPlaceHolder.Controls.Add(container);
}
}
public class MessageContainer : Control, INamingContainer { }
}
}
What changes can I make to my MinimalTemplate code so that Visual Studio will validate and autocomplete its ContentTemplate tag?
Related.
Add [PersistenceMode(PersistenceMode.InnerProperty)] to ContentTemplate's attribute list. After adding it and rebuilding, the validation error disappeared and "ContentTemplate" appeared as expected in the Intellisense dropdown.
During my investigation, I'm certain I tried adding this property two or three times to no effect, so I expect the VS validator is a bit flaky. It smacks of voodoo programming, but do a Clean/Rebuild All and wait a few seconds before seeing whether the validation error persists.
(Also, you don't need the ParseChildren attribute for this control.)

ASP.NET UserControl Inheritance

I have a UserControl that is working fine. It is declared like this.
public partial class DynamicList : System.Web.UI.UserControl
{
protected static BaseListController m_GenericListController = null;
public DynamicList()
{
m_GenericListController = new GenericListController(this);
}
}
Now I want to override this control so I can change some of the properties. I have created a class like this.
public partial class JobRunningList : DynamicList
{
public JobRunningList()
{
m_GenericListController = new JobListController(this);
(m_GenericListController as GenericListController).ModuleId = 14;
}
}
It appears that the controls in the DynamicList are not getting created though when I use the JobRunningList control now causing predictably bad results. The DynamicList UserControl has a ListView on it and a few other controls. It appears these are not created when using the JobRunningList. Is there any secret to this?
The boring workaround would be to make JobRunningList as plain old user control that contains a DynamicList and just sets the properties of the inner control in its OnLoad. That's awkward if DynamicList has many other properties that you want to access from the page though, as JobRunningList would have to define matching properties of its own. Getting back to the inheritance approach, then...
The DynamicList class just contains the code behind logic, so what you're doing works nicely if you want the second control to reuse the logic behind the first but provide a new UI of its own.
The markup in your .ascx file gets compiled into another class that inherits DynamicList, so if you can get your JobRunningList class to inherit that class instead of DynamicList, you'll get the result you want. This class gets a default name derived from the filename, but you can avoid guessing that by setting a ClassName in the control directive to use instead of the automatic name.
Take a simple base control like
<%# Control Language="C#" AutoEventWireup="true"
CodeFile="HelloControl.ascx.cs" Inherits="HelloControlBase"
ClassName="MyControls.HelloControl" %>
Hello <%= Name %>
with an unexciting code-behind like
public partial class HelloControlBase : System.Web.UI.UserControl
{
public string Name
{
get;
set;
}
}
Now we want to override the Name property in a new control. First we need HelloAlice.ascx
<%# Control Language="C#" AutoEventWireup="true"
CodeFile="HelloAliceControl.ascx.cs"
Inherits="HelloAliceControl" %>
Not much to see here, since we're leaving all the work to the original ascx. Now in the code-behind,
public partial class HelloAliceControl : MyControls.HelloControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.Name = "Alice";
}
}
We just inherit MyControls.HelloControl and set the Name property, and it looks like we're done.
The problem is knowing when MyControls.HelloControl is visible. As long as your derived control is in the same directory as the parent control you'll probably be OK, otherwise it's quite easy to run into build errors complaining that the class doesn't exist because the parent control hasn't been compiled yet.
If I understand correctly, you want the interface to be the same. In that case, I would create some properties instead. Perhaps just a simple enumeration i.e. ListType.

ASP.NET- Instantiate a Web User Control in App_Code class

Files:
Website\Controls\map.ascx
Website\App_Code\map.cs
I'd like to create a strongly typed instance of map.ascx in map.cs
Normally, in an aspx, you would add a <%Register... tag to be able to instantiate in codebehind. Is this possible in an app_code class?
I'm using .NET 3.5/Visual Studio 2008
Thanks!
Normally, I'd do something like this (assuming your type is "Map" and that you have the appropriate "Inherits" declaration in your .ascx file):
Map map = (Map)LoadControl("~/Controls/map.ascx");
Is there a map.ascx.cs file in Website\Controls? If so, move it to App_Code. Note you may have to update the CodeFile attribute in the .ascx file to ~\App_Code\map.ascx.cs.
Alternatively, since the control is a partial class, you could just create the code in ~\App_Code\map.cs as:
public partial class controls_Map : UserControl
{
protected void Page_Load( object sender, EventArgs e )
{
...code here....
}
}
And remove all the methods from the map.ascx.cs file in the controls directory.

Resources