Dynamically access master pages properties - asp.net

We can change masterpagefile for particular page dynamically.but how can i access their properies which are changing as per the master pages.
what code will give me their properties.
Thanks

You can add a public property to the code behind of your master page, like so:
public string MyMasterPageProperty {
return "my stuff";
}
Then, on the content page, you can use this code to access your new property.
Page.Master.MyMasterPageProperty;
If you don't want to add the MasterType directive on the aspx page, you can always explicitly cast the Page.Master to the type of your master page, so that it looks like this:
((MyMasterPageClass)Page.Master).MyMasterPageProperty;
You can read more info here:
https://web.archive.org/web/20210513005959/https://www.4guysfromrolla.com/articles/013107-1.aspx

You'll need the MasterType directive on the aspx page:
<%# MasterType VirtualPath="~/Site1.Master" %>
Then on your aspx.cs page, you can call the properties of the master:
Master.Property1 = "whatever you wanna do";
If you need more instructions:
http://dotnet.dzone.com/news/back-basics-%E2%80%93-using-mastertype

Related

error Couldn't load type 'MyMaster'?

I have multiple master pages in my application. I made a custom type and inharit all master pages from it:
public class MyMaster : System.Web.UI.MasterPage
{
private int myno;
public int MyNo
{
get { return 50; }
set { myno = value; }
}
}
Basically myno is common in all master pages and master pages will be changing in content pages. To add this property I added in my content page:
<%# MasterType TypeName=MyMaster %>
Running on page I got subject error. Kindly guide me.
Thank
If you're working in a web site, you should typically put a derived MasterPage class in the application's App_Code folder. When VS compiles your site, classes in the AppCode folder are compiled into the site's namespace and available to the code-behind pages of the application.
If you're using a web application, you should be able to put the class file in the root of the site and reference it directly from the code-behind of any other page in the project, assuming all namespace references are correct.
Also make sure that your the Build Action for your class file is set to Compile. You can check this in the properties window by selecting the class file in solution explorer.
I just had this problem.
Turns out the MasterPageFile attribute of my Page directive didn't match the TypeName in my MasterType directive.
In other words, I gave the TypeName for the wrong master page file. Oops!
Here is a demonstration of the problem.
<%# Page Title="" Language="VB" MasterPageFile="~/Departments/SomeMaster.master" AutoEventWireup="false"
CodeFile="ExamplePage.aspx.vb" Inherits="Departments_ExamplePage" Theme="Default" %>
<%# MasterType TypeName="Departments_SomeOtherMaster" %>

Accessing ASP Master Page Properties

I have an ASP.net page which uses a master page. The master page has a public property. Does anybody know how can I access the given property from a content page that uses the master page?
Look into Strongly Typing your Master Page link
Check out the <%# MasterType virtualPath="~/MasterPage.master"%> tag.
This enables you to directly have access to the public properties/methods.

Create a usercontrol instance programmatically in ASP.NET

I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success....
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.
I added a reference to the control in the .aspx page that will use it, like this:
<%# Reference Control="~/PictureGallery.ascx" %>
And in the UserControl I added a ClassName like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>
When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".
The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck.
So, I'm not able to get a reference to the UserControl.
How can it be fixed?
It sounds like you are confusing two separate ways of working with a UserControl.
One way is to register the control on your page so that you can put it into the page at Design Time e.g.
<div>
<asp:PictureGallery id="myGallery" runat="server" MyProperty="SomeValue">
</asp:PictureGallery>
</div>
The second is programmatically (or dynamically) adding it into the page at runtime in your code behind. If so, then you need to use the LoadControl function which is mentioned in the sample. You do not need to register the control in the aspx file if you do this.
e.g.
Dim gallery as PictureGallery = LoadControl("~/PathToControl/gallery.ascx")
gallery.MyProperty = "SomeValue"
placeHolder.controls.add(gallery)
edit
What is the class name of the control in the code behind...something like this:
Partial Public Class MyControlsClassName
Inherits System.Web.UI.UserControl
That is the type you need to use when you declare it. Is it within a namespace perhaps?
I don't think you've placed the control in your code behind. You may well have created the reference, but do you have a tag such as <asp:PictureGalary id="gallary"></asp:PictureGalary> anywhere in your aspx?
The ASP namespace is generated at run time- user controls get "compiled" as they are used by .aspx pages so this is why you get the error message "Type ASP.PictureGallery is not defined".
When adding user controls dynamically you should use the Page.LoadControl method:
Page.LoadControl("~/PictureGallery.ascx")

How to add code behind manually on asp.net

I have a website on the IIS but it has only the aspx file. Now I need to add the code behind for some pages. How do I go about this?
I've been trying to add the attribute "codebehind" and "autoeventwireup" on the top of the aspx file but no luck (the page_load event is not being called). Also, if I double click on the button from the design view in Visual Studio, it creates the javascript handle (not the server code).
In your page directive on the aspx page (<%# Page...), you need two pieces:
1) CodeFile="Default.aspx.vb"
2) Inherits="_Default"
In your code-behind file, you need two things:
1) The file name has to be whatever you specified above (CodeFile = "...)
2) the class name has to be whatever you specified above (Inherits="_...)
For example, if your aspx page was named "default.aspx" then your page direction in the aspx page would look similar to this:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
And your code-behind would look similar to this:
Partial Class _Default
Inherits System.Web.UI.Page
End Class
you want the #page directive and the class attribute. MSDN docs here

How to access controls inside a nested master page? why does it behave differently from content pages?

Is there a difference between these two scenarios:
(1) Accessing a property on a master page from a regular child
(2) Accessing a property on a master page from a nested master page
I tried to access a textbox in the master page from a content page like this:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
defaultTextbox.Text = a.Text; // defaultTextBox is a textbox control inside default.aspx
it works, but then when I applied the same method on a nested master page:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
myTextBox.Text = a.Text; // myTextBox is a textbox control inside child.master
this does not work, am I missing something? I call both codes inside regulare page_load handler...
I also noticed I cannot set textbox value inside the nested master page from code behind, there is definitely something im missing, what is it?
To shed light on this issue, here is an example:
Nested Master Page:
<%# Master Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:textbox id="tx2" runat="server" text="this is two"></asp:textbox>
<asp:contentplaceholder id="newstuff" runat="server"></asp:contentplaceholder>
</asp:Content>
Code behind:
Response.Wrote(tx2.Text);
I get NOTHING, why what did I miss? note that I also tried the recursive find control:
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
still nothing
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
//base content place holder id
Label objLabel3 = (Label)cp.FindControl("lblNested");
//lblNested is id in nested master page
I read few things here:
http://www.odetocode.com/Articles/450.aspx
and found out that the nested page in the middle never calls Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following:
protected override void OnLoad(EventArgs e)
{
myTextBox.Text = "anything";
base.OnLoad(e);
}
This should work without any problems, so something else must be wrong. I just tried it inside a simple test project and I have no problems finding a control on the master page in both cases.
I would check (again) if you refer to the correct master page inside your nested master page. What you could also check is the runtime type of the Master property inside your nested master page. It should be the type of your master page.
EDIT: I thought the issue was about finding a control in the root master page from a nested master page and this should work without any problems. For finding a control inside a content placeholder in a nested master page, take a look at the following forum post.
You can have absolute control of your content in both master page and nested page from your content page using the directives:
<%# MasterType VirtualPath="your_master.master" %>
<%# Reference VirtualPath="~/your_master.master" %>
See the excellent article from K. Scott Allen in Ode To Code

Resources