access master page property from class file - c#

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.

Related

How to set public bool value in C# code behind, read in .ascx User Control?

I want to slightly modify a user control that is being loaded by various .aspx pages.
If I set a bool value in the .aspx.cs page
public bool showTemplateBGOption;
public bool TemplateBGOption
{
set { showTemplateBGOption = false; }
}
How can I read the bool value showTemplateBGOption in the .ascx control that is rendered on the same .aspx page?
You could cast the Page property to the right type, the page property needs a getter.
(somewhere in your ascx codebehind):
MyPageType page = this.Page as MyPageType;
if(page != null)
{
bool templateBGOption = page.TemplateBGOption;
}
But in this way you are hard-wiring the page with the UserControl. Instead the page should specify the TemplateBGOption to the UserControl.
(somewhere in your page's codebehind):
this.UserControl1.TemplateBGOption = this.TemplateBGOption;
You can inplement an Interface to All Pages that will use this property and then use it in the user control and in this way you can decouple a user control from a specific page but you do it to a "contract"
interface ITemplateBOption
{
bool TemplateBGOption{get;set;}
}
Public MyPage : Page, ITemplateBOption
{
public bool TemplateBGOption
{
get{...}
set{...}
}
}
In your User Control use like this:
ITemplateBOption page = this.Page as ITemplateBOption;
if(page != null)
{
bool templateBGOption = page.TemplateBGOption;
}else
{
//do something, thrown an exception for example.
}

MasterPage Access Controls

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.

MasterPage objects returning as null

I've got an ASP.net application that is used to display information queried from our ERP system onto various web pages. The main object is called an EpicorUser, and it basically encapsulates all current information about an employee.
This object is used to fill in a bunch of various fields on the master page such as Full Name, current activity, clock in/out times, etc.
I am trying to pass this object from the MasterPage into the content pages to avoid needlessly querying the WebService that serves this information. The problem is, when I access the object from a ContentPage, it is always null. I know it has been populated because my MasterPage content is all filled in correctly.
I am trying to access the MasterPage 'CurrentUser' object from my ContentPage like this:
**MasterPage Codebehind:**
public EpicorUser CurrentUser; //This object is populated once user has authenticated
///This is in my ContentPage ASPX file so I can reference the MasterPage from codebehind
<%# MasterType VirtualPath="~/Pages/MasterPage/ShopConnect.Master" %>
**ContentPage CodeBehind:**
string FullName = Master.CurrentUser.UserFileData.FullName; //CurrentUser is null(but it shouldn't be)
Strange thing is, I had another content page where this system worked fine. It has also stopped working, and I don't think I have changed anything on the masterpage that could cause this. I had set the CurrentUser as a public property so I could access
I went as far a creating a method to re-populate the object from the master page, and calling it from the code-behind on the contentpage:
**ContentPage code-behind:**
EpicorUser CurrentUser = Master.GetCurrentUserObject();
**MasterPage Method being invoked:**
public EpicorUser GetCurrentUserObject()
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "OFFICE"))
{
UserPrincipal principal = UserPrincipal.FindByIdentity(context, HttpContext.Current.User.Identity.Name);
EpicorUser CurrentEmployee = RetrieveUserInfoByWindowsID(principal.SamAccountName);
return CurrentUser; //Object is NOT null before the return
}
}
**ContentPage code-behind return:**
EpicorUser CurrentUser = Master.GetCurrentUserObject(); //But object is now null once we return
Stepping through the code shows me that the CurrentUser object is populated correctly in the MasterPage code behind, but once it is returned to the ContentPage code behind, it is now null!
Anyone know where the disconnect is?
Content Page is loaded first and then Master page will be loaded. So, your property could be blank when it is accessed in the content page. You can try creating a public method(to return UserObject) on the master page and then call the method from content page.
Another option is
creating a base page class(inherit all content pages) and create a property to return the user object. So, all pages can access the value
EDIT:
public class BasePageClass : System.Web.UI.Page
{
public List<string> LookupValues
{
get
{
if (ViewState["LookupValues"] == null)
{
/*
* create default instance here or retrieve values from Database for one time purpose
*/
ViewState["LookupValues"] = new List<string>();
}
return ViewState["LookupValues"] as List<string>;
}
}
}
public partial class WebForm6 : BasePageClass
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void MyButton_Click(object sender, EventArgs e)
{
//access lookup properties
List<string> myValues = LookupValues;
}
}

UserControl pass Value to MasterPage Content

I am coding in a userControl,
how can I pass text to a control I placed in one of the contents?
It's a literal by the way
Thank you
Create a public property in Master page.
public string WhateverPropertyName
{
get
{
return LiteralControlInMasterPage.Text;
}
set
{
LiteralControlInMasterPage.Text = value;
}
}
and then access it from the usercontrol as
Page.Master.WhateverPropertyName = "Whatever new value";
If you cannot access/update master page use
Literal mpLiteral = (Literal) Master.FindControl("masterPageLiteralControlID");

Access Master Page public method from user control/class/page

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 = ...;

Categories

Resources