I have created a menu control in asp.net and have put it into master page. This menu control have property ActiveItem which when modified makes one or another item from menu appear as active.
To control active item from child page I have created a control which modifies master page property enforced by IMenuContainer interface to update menu control's active item in master page.
public class MenuActiveItem : Control
{
public ActiveItemEnum ActiveItem
{
get
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return ActiveItemEnum.None;
return masterPage.ActiveItem;
}
set
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return;
masterPage.ActiveItem = value;
}
}
}
Everything works perfectly and I really enjoy this solution, but I was thinking that, if I knew how, I would have created a custom directive with same feature instead of custom control because it just makes more sense that way.
Does anybody know how to do it?
You should be able to turn this into a custom property of your Page, which you can set in the Page directive.
Create a base class for your page, and then change your Page directives like this:
<%# Page Language="C#" MasterPageFile="~/App.master"
CodeFileBaseClass="BasePage" ActiveItem="myActiveItem" AutoEventWireup="true"
CodeFile="Page1.aspx.cs" Inherits="Page1" %>
You may have to change the property to be a string, and do a conversion to the enum. But otherwise your code can remain the same, and it doesn't have to be in a control.
Related
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.
If the master page has a label with the id label1 how do I control that id in the content page. The id is not passed down so i can't control it inherently. For example if i have a control with the id contentLabel i can access it code by just typing contentLabel.(whatever i'm doing)
Here are two options:
1: make sure your content aspx specifies MasterType:
<%# MasterType VirtualPath="~/yourMasterPageName.master" %>
Doing this lets your content page know what to expect from your master-page and gives you intellisense. So, now you can go ahead and expose the label's Text property on the master page's code-behind.
public string ContentLabelText
{
get { return contentLabel.Text; }
set { contentLabel.Text = value; }
}
Then you can access it in your content page's code-behind page ala:
Master.ContentLabelText = "hah!";
or, 2: You can access the label via FindControl() like so:
var contentLabel = Master.FindControl("contentLabel") as Label;
Im trying to access a ModalPopupExtender control and it allways is returning null or object set not set to an intance of an object. I've tried master.Page.FindControl("") and master.FindControl("") and im still not getting the result
MasterPage master = Page.Master as MasterPage;
AjaxControlToolkit.ModalPopupExtender popup = master.Page.FindControl("ModalPopupExtender2") as AjaxControlToolkit.ModalPopupExtender;
Updated: Cannot change the text of my labels in master page
MasterPage master = Page.Master;
AjaxControlToolkit.ModalPopupExtender popup1 = master.FindControl("ModalPopupExtender1") as AjaxControlToolkit.ModalPopupExtender;
Label lblMessage = master.FindControl("lblMessage") as Label;
lblMessage.Text = msg;
Literal ltrlMessage = master.FindControl("ltrlMessage") as Literal;
ltrlMessage.Text = msg;
Label MessageStatus = master.FindControl("lblMessageStatus") as Label;
MessageStatus.Text = msgStatus;
popup1.Show();
you could do
MasterPage master = Page.Master;
If your page is a child of your master page
try this:
AjaxControlToolkit.ModalPopupExtender popup = (AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("ModalPopupExtender2");
Regards
Check out this answer. You can have a strongly typed master page, so you don't have to find and then cast the control. The control on the master would be publicly accessible, and the page would know the type of the master page and have it accessible.
EDIT:
the control is not public
Assuming you've set the Master property in your page directive:
<%# Page MasterPageFile="~/MyMaster.master" ...
Odds are, you probably don't need to actually get to the control. Rather, you need to set something in the master page. I'd just use an internal method to do what you need to do:
public partial class MyMaster: MasterPage
{
internal void SetTheFoo(string foo)
{
this.WhateverControl.Text = foo;
}
//etc...
}
Then, from your page, just call it:
Master.SetTheFoo("Foo");
If you still need to get to the control, then in your master page, you could add a public property exposing your modal popup extender.
public AjaxControlToolkit.ModalPopupExtender MyModalPopup
{
get { return this.TheNonPublicModalPopupExtenderControl; }
}
I'm curious to know that using .net 2.0 with a master page if there is a way that I can pick up what page I am on so that i can use it to style a tab?
My master page has a nav bar on it, and what I wan to do is:
If the user is, say on the contact page, that the tab for the contact page would be a different color, can this be achieved. I have seen some examples that don't use master pages and of course you can use the encapsulating body tag to signify where you are but this isn't available with a masterpage.
Thanks R.
MasterPage though the name sound otherwise behaves like a child to a page that uses it.
Think of it as a UserControl to a page. You can actually access to the Page instance and it's Request property.
Here's an example on how you can use it
switch(Request.Path){
case "/page1/aspx":
//dosomething to your tabs
break:
case "/page1/aspx":
//dosomething to your tabs
break:
.
.
.
default:
//dosomething else
.
.
.
}
If you want to change the content on the masterpage from the page (i.e. change the tab color) you should:
In the masterpage, publicly expose a property or method that will change the color of the tab.
i.e.:
public void changecolor(string PageName, string Color){
switch(PageName){
case "home":
this.TabHome.Color=Color;
}
}
Then put a directive at the top of the aspx page with the masterpage path. Like such:
<%# MasterType VirtualPath="~/Site.master" %>
Once this is done, from the codebehind, you can access the masterpage and see its exposed method, then just call this and you're done.
protected void Page_Init(object sender, EventArgs e){
Master.changecolor("home", "red");
}
this way, you won't have to parse pagenames and deal with the maintenance that comes when you try to change the name of the page etc. you will also limit your case statement to the number of tabs, and not the number of pages in your site.
Create the following method in your masterpage (or helper class) and then add a reference to it in your Page_Load method in the masterpage:
public string GetCurrentPageName()
{
Uri uri = Request.Url;
string[] uriSegments = uri.Segments;
string pageName = "";
if( 0 < uriSegments.Length )
{
pageName = uriSegments.Last();
}
return pageName;
}
}
That should give you the current filename - you might want to strip out the ".aspx" part of the filename also. I haven't tested this with a QueryString yet so not sure if Last() still returns the filename in that case.
If your tabs are asp.net controls, you can use FindControl() to find the tab - you'll need to match your tab ids with your page names of course. Once you have the control you can add a "selected" style in code-behind.
Is there a way to get a value I am storing in a Master Page hidden field from a User Class which I created and placed in the App_Code folder of my ASP.Net 2.0 Application?
Some examples would preferably in VB.Net is highly appreciated.
Thanks.
To give further details, assume the following:
MasterPage.Master
MasterPage.Master.vb
MyPage.aspx
Mypage.aspx.vb
IN the app_code folder, add a new class, say TESTClass.
I have placed some logic in master page. MyPage.aspx uses the Masterpage.master as its master page. In the master page, the logic which I did stores a value into a hidden field.
in my TestClass, how do I access the master page hidden field?
Please take note that TestClass is NOT a user control but a user defined class, which contains some Business-Specific logic which is accessed by myPage.aspx.vb.
I tried ScarletGarden's suggestion but it did not seem to get the Masterpage Hiddenfield which I need to get the value.
Would something like this work?
((HiddenField)this.Page.Master.FindControl("[hidden control id]")).Text
You can get it by these :
hiddenControlValue = HttpContext.Current.Request["hiddenControlId"]
or you can pass your page to your method that belongs to your class under App_Config, and reach it as :
public static string GetHiddenValue(Page currentPage)
{
return currentPage.Request["hiddenValue"];
}
or you can get it over context :
public static string GetHiddenValue()
{
return HttpContext.Current.Request["hiddenValue"];
}
hope this helps.
EDIT: I re-read the question after answering, and realize my answer was probably not quite what you were after. :/
Jared's code might work, but you can also try the following.
In your MasterPage, make the HiddenField a public property, and store the content in the ViewState to make keep it during post backs.
Something like so:
public HiddenField theHiddenField
{
get
{
if (ViewState["HiddenField"] == null)
return null; //or something that makes you handle an unset ViewState
else
return ViewState["HiddenField"].ToString();
}
set
{
ViewState["HiddenField"] = value;
}
}
You then have to add the following to your ASCX-file:
<%# Reference Control="~/Masterpages/Communication.Master" %>
You then access it thusly.
Page mypage = (Page) this.Page; // Or instead of Page, use the page you're actually working with, like MyWebsite.Pages.PageWithUserControl
MasterPage mp = (MasterPage) mypage.Master;
HiddenField hf = mp.theHiddenField;
Sorry if the answer got a bit messy. This is, of course, how to do it in C#, if you want to use VB have a look at this link for the same idea.