I am to access a method on my master page. I have an error label which I want to update based on error messages I get from my site.
public string ErrorText
{
get { return this.infoLabel.Text; }
set { this.infoLabel.Text = value; }
}
How can I access this from my user control or classes that I set up?
To access the masterpage:
this.Page.Master
then you might need to cast to the actual type of the master page so that you could get the ErrorText property or make your master page implement an interface containing this property.
Page should contain next markup:
<%# MasterType VirtualPath="~/Site.master" %>
then Page.Master will have not a type of MasterPage but your master page's type, i.e.:
public partial class MySiteMaster : MasterPage
{
public string ErrorText { get; set; }
}
Page code-behind:
this.Master.ErrorText = ...;
Another way:
public interface IMyMasterPage
{
string ErrorText { get; set; }
}
(put it to App_Code or better - into class library)
public partial class MySiteMaster : MasterPage, IMyMasterPage { }
Usage:
((IMyMasterPage )this.Page.Master).ErrorText = ...;
Related
How do I access Master page property from .cs file? I tried the following code but I couldn't access it.Please let me know.
Master Page property:
public int TypeID
{
get
{
return Convert.ToInt32(this.ViewState["TypeID"]);
}
set
{
this.ViewState.Remove("TypeID");
this.ViewState.Add("TypeID", value);
}
}
data.cs
var pageHandler = HttpContext.Current.CurrentHandler;
if (pageHandler is System.Web.UI.Page)
{
typeId = Convert.ToInt32((System.Web.UI.Page)pageHandler).Master.TypeID;
}
The Master property of a page is typed as System.Web.UI.MasterPage. In order to see the TypeId property, you need to cast the Master to the type of your specific master page.
var page = (System.Web.UI.Page)pageHandler
var master = (MyMasterType)page.Master; //Replace MyMasterType with the class name from your masterpage.cs file.
var typeId = master.TypeId;
You'll want to be careful casting it to a specific master type if you have multiple master types in your application or if some pages don't have a master page.
I have 2 master page in my web site.
MainMaster
SubMaster (Master Page:MainMaster)
Page (Master Page:SubMaster)
I have hidden fields on SubMasterPage. And i'm proccessing datas and setting hidden field value on SubMasterPage Init event. I want to get hiddenfield's value from Page.aspx
I'm trying this on Page.aspx, getting "Object reference not set to an instance of an object." error
((HiddenField)this.Master.FindControl("hiddenId")).Value
But when i have 1 master page this code works normally.
Have i a solution in this problem? Or should i try transfer datas via session/querystring e.t.c.?
You could add a property to your sub master page to return the value, and use this in the child page.
eg.
Sub Master Page
public string HiddenValue
{
get
{
//return the value of your hidden field
return HiddenID.Value;
}
}
Child page:
//Method to get the hidden value from the master page, if the master page is a sub master page
private string GetHiddenValue()
{
if (this.Master is SubMasterPage)
{
string value = (this.Master as SubMasterPage).HiddenValue;
return value;
}
else
{
return string.Empty;
}
}
if you want to go one step further you could add an extension method to the MasterPage class to call it easily from any page.
eg:
public static class MasterPageExtensions
{
public static string GetHiddenFieldValue(this MasterPage master)
{
if (master is SubMasterPage)
return (master as SubMasterPage).HiddenFieldValue;
else
return string.Empty;
}
}
public class SubMasterPage : MasterPage
{
private HiddenField _hiddenField;
public string HiddenFieldValue
{
get
{
return _hiddenField.Value;
}
}
}
public class ChildPage : Page
{
void TestMethod()
{
string hiddenValue = this.Master.GetHiddenFieldValue();
}
}
this is particularly useful when for example you have a single modal popup message box on the master page, and you want to show it from any child page.
i want to update a user control from content page, and user control is in master page.
anybody have any idea about it ?
Thanks in advance,
Add property in your UserControl like this:
public int ItemCount{ get;set; }
public string CountText
{
get{ return labelId.Text; }
set{ labelId.Text = "Item in shopcart: "+ ItemCount; }
}
then in your aspx make on add item to cart (button click or something whatever you want)
YouUserControlId.ItemCount++;
What you have to do is in your user control Expose the label (make it public).
public class MyUCtrl : UserControl{
public Label MyLabel{ get; set; }
...
}
Then in the Master Page do something similar:
public MyUCtrl Counter{ get; set; }
...
Then in the content page:
((MyMasterPage)this.Master).Counter.MyLabel.Text = "hello";
I have quite simple site structure with one mastepage and a bunch of pages. The mastepage is quite advanced though and I need from the page be able to control certain aspects of the Masterpage.
I want to be able to enter these directive in the aspx file to not clutter the code behind files.
My idea was to create different "directive" user controls such as SeoDirective:
using System;
public partial class includes_SeoDirective : System.Web.UI.UserControl
{
public string Title { get; set; }
public string MetaDescription { get; set; }
public string MetaKeywords { get; set; }
}
I include this directive in those pages that need to override the default mastepage settings.
<offerta:SeoDirective runat="server" Title="About Us" MetaDescription="Helloworld"/>
In my masterpage I look if there's any directives:
includes_SeoDirective seo = (includes_SeoDirective) ContentPlaceHolder1.Controls.Children().FirstOrDefault(e => e is includes_SeoDirective);
(Children() is an extension so I can work with Linq on a ControlCollection)
Now to my question: I'm not to happy about this solution might be a bit bloated?
I'm looking for alternative solutions where I can created these tags in the aspx file.
I've looked at the trick where I extend the Page, but that requiries we to modify the VS configs for the project to compile, so I dropped that solutions.
As far as I am aware, there isn't a standard way of doing this. I have done this same thing in the past in much the same way you have, except I used an interface on the pages that I needed the master page to look for, which defined a method it could call to do specific logic with the master page.
You might be able to use this same paradigm:
ISpecialPage.cs:
public interface ISpecialPage
{
string Title { get; set; }
string MetaDescription { get; set; }
string MetaKeywords { get; set; }
}
MyPage.aspx:
public partial class MyPage : System.Web.UI.Page, ISpecialPage
{
public string Title { get; set; }
public string MetaDescription { get; set; }
public string MetaKeywords { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Some title";
this.MetaDescription = "Some description";
this.MetaKeywords = "Some keywords";
}
}
MasterPage.master:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.Context.Handler is ISpecialPage)
{
ISpecialPage specialPage = (ISpecialPage)this.Context.Handler;
// Logic to read the properties from the ISpecialPage and apply them to the MasterPage here
}
}
}
This way you can handle all MasterPage logic in the master page code behind file, and simply use the interface on pages you need to provide certain information.
Hope this helps you!
I'm working on a .net 3.5 site, standard website project.
I've written a custom page class in the sites App_Code folder (MyPage).
I also have a master page with a property.
public partial class MyMaster : System.Web.UI.MasterPage
{
...
private string pageID = "";
public string PageID
{
get { return pageID; }
set { pageID = value; }
}
}
I'm trying to reference this property from a property in MyPage.
public class MyPage : System.Web.UI.Page
{
...
public string PageID
{
set
{
((MyMaster)Master).PageID = value;
}
get
{
return ((MyMaster)Master).PageID;
}
}
}
I end up with "The type or namespace name 'MyMaster' could not be found. I've got it working by using FindControl() instead of a property on the MyMaster page, but IDs in the master page could change.
I've tended to do the following with Web Site projects:
In App_Code create the the following:
BaseMaster.cs
using System.Web.UI;
public class BaseMaster : MasterPage
{
public string MyString { get; set; }
}
BasePage.cs:
using System;
using System.Web.UI;
public class BasePage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (null != Master && Master is BaseMaster)
{
((BaseMaster)Master).MyString = "Some value";
}
}
}
My Master pages then inherit from BaseMaster:
using System;
public partial class Masters_MyMasterPage : BaseMaster
{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(MyString))
{
// Do something.
}
}
}
And my pages inherit from BasePage:
public partial class _Default : BasePage
I found some background to this, it happens because of the way things are built and referenced.
Everything in App_Code compiles into an assembly.
The rest, aspx files, code behind, masterpages etc, compile into another assemlby that references the App_Code one.
Hence the one way street.
And also why Ben's solution works. Thanks Ben.
Tis all clear to me now.
I realise there are already accepted solutions for this, but I just stumbled across this thread.
The simplest solution is the one listed in the Microsoft website
(http://msdn.microsoft.com/en-us/library/c8y19k6h.ASPX )
Basically it says, your code will work as-is, if you include an extra directive in the child page aspx:
<%# MasterType VirtualPath="~/MyMaster.Master" %>
Then you can directly reference the property in the base MyPage by:
public string PageID
{
set
{
Master.PageID = value;
}
get
{
return Master.PageID;
}
}