it is very easy to access master page control from content page like
protected void Page_Load(object sender, EventArgs e)
{
// content page load event
DropDownList thisDropDown = this.Master.FindControl("someDropDown") as DropDownList;
userLabel.Text = thisDropDown.SelectedValue;
}
but how could i access controls of content page from master page. suppose a textbox there in content page and one button is there in master page. i want that when i will click on master page button then i want to show the text of textbox in the content page in the label of master page. how to achieve it. please help me with code sample. thanks.
In master page button click event should access page contents by:-
protected void Button1_Click(object sender, EventArgs e)
{
TextBox TextBox1 = (TextBox)ContentPlaceHolder1.FindControl("TextBox1");
if (TextBox1 != null)
{
Label1.Text = TextBox1.Text;
}
}
It's been a while, but I believe you can do so by using the ContentPlaceHolder as a reference:
Control control = this.myContentPlaceHolder.FindControl("ContentPageControlID");
In my opinion it even better to use event raise from Master page and catch this event in contenet page for changing some contenet on this page, for instance. The main advantage is reusability. In future you may want to change content on other content page from the Master page and in this case you should only add event handler to this content page without changing code on master page. Within such approach you needn't hardcode control name from some content page. And moreover you shouldn't add dependency for some content's control at all.
A sample of implementation you can find here, for example.
you should look for contentplaceholder from master page then contentplaceholder in child of the master page
this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
You can find control by using this:
ContentPlaceHolder contentPage = Page.MasterPage.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
Label lblHead =(Label)contentPage.FindControl("lblHeading");
Response.Write(lblHead.Text);
Source:
http://xpode.com/ShowArticle.aspx?ArticleId=629
C# code within Site.Master:
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
Code within Site.Master.cs:
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
ToolkitScriptManager1.RegisterPostBackControl(this.MainContent.FindControl("btnOnDefaultPage"));
}
}
This is an example how a client control on the Default.aspx is referenced from a Site.Master.cs
Try this code
Page.Master.FindControl("MainContent").FindControl("DivContainer_MyProfile").Visible = True
Related
On loading of every content page, I want to get content page name. So I want to know that which Master Page event is fired on every content page load ?
Here's a resource that might help, it states the events that ocur for ASP.NET pages:
http://weblogs.asp.net/ricardoperes/archive/2009/03/08/asp-net-page-events-lifecycle.aspx
Page.OnPreInit
MasterPageControl.OnInit (for each control on the master page)
Control.OnInit (for each contol on the page)
MasterPage.OnInit
Page.OnInit
Page.OnInitComplete
Page.LoadPageStateFromPersistenceMedium
Page.LoadViewState
MasterPage.LoadViewState
Page.OnPreLoad
Page.OnLoad
MasterPage.OnLoad
MasterPageControl.OnLoad (for each control on the master page)
Control.OnLoad (for each control on the page)
OnXXX (control event)
MasterPage.OnBubbleEvent
Page.OnBubbleEvent
Page.OnLoadComplete
Page.OnPreRender
MasterPage.OnPreRender
MasterPageControl.OnPreRender (for each control on the master page)
Control.OnPreRender (for each control on the page)
Page.OnPreRenderComplete
MasterPageControl.SaveControlState (for each control on the master
page)
Control.SaveControlState (for each control on the page)
Page.SaveViewState
MasterPage.SaveViewState
Page.SavePageStateToPersistenceMedium
Page.OnSaveStateComplete
MasterPageControl.OnUnload (for each control on the master page)
Control.OnUnload (for each control on the page)
MasterPage.OnUnload
Page.OnUnload
Also here is the official documentation about ASP.NET page lifecycle which goes into detail about all the events. Hopefully this will help you.
EDIT;
Hmmm, actually the above looks a bit over the top. It looks like all you need to do is - in each content page, make sure you reference the master page in the ASPX file:
<%# MasterType virtualpath="~/Masters/Master1.master" %>
Then in the master page have a public method such as:
public void LogContentPageName(string name)
{
// Do whatever you want with the passed name.
}
Then in the Page_Load event of the content pages you can do:
protected void Page_Load(object sender, Eventargs e)
{
Master.LogContentPageName("Whatever");
}
Please try with below event
protected override void OnInit(EventArgs e) {
//do your stuff here }
Read this thread but didn't really answer my question and there were quite a few suggestions so not sure if they are on the right track: Master Page content filtering with respect to asp page
What I have is a site with 1 Master Page and in it is a global footer that all pages use. I want to eliminate the footer on only 1 page (i.e. the login page) but keep all the other master page content intact.
I know I could create a separate Master Page just for this login page but it seems overkill. Is there a way to put in some logic that if it's only this specific page that it would hide the footer and then show on every other page?
Thanks for any tips/suggestions.
Edit: There was already a Page Load sub in the code behind. All I had to add was - MasterPage_Footer.Visible = False on the If statement when users were not logged in and set it to True when they were logged in. Works like a charm. Thanks for all the suggestions.
Expose a property on the MasterPage to allow content pages to override default behavior if needed.
In the MasterPage:
private bool showFooter = true;
public bool ShowFooter { get {return showFooter;} set {showFooter = value;} }
protected void Page_Load(object sender, EventArgs e)
{
footerControl.Visible = showFooter;
}
Make sure content pages that need to access the property have the following line in the aspx:
<%# MasterType TypeName="XXX" %>
and in the content pages code-behind:
protected void Page_Load(object sender, EventArgs e)
{
Master.ShowFooter = false;
}
In your MasterPage:
protected void Page_Load(object sender, EventArgs e)
{
var page = HttpContext.Current.Handler as Page;
FooterControl.Visible = HttpRequest.IsAuthenticated && !(page is LoginPage)
}
HttpContext.Handler Property
is (C# Reference)
how to Bind webpages that's already created with a new master page ?
AT Page_PreInit event you can set master page.
for example
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "Master.master";
}
You will need to change the markup in the .aspx file something along the following lines
1) Remove html head & body
2) Insert ASP:CONTENT tag
3) Insert MasterPageFile attribute in #PAGE directive
To be honest the easiest way might be to just create a new content page, select the masterpage and then copy any markup you need into the panel and copy your codebehind over too.
I have a Treeview in a masterpage and a products page in child page.
When i click treeview node i want to bind data to a gridview on the product page.
protected void trvCategoryTab_SelectedNodeChanged(object sender, EventArgs e)
{
if (trvCategoryTab.SelectedNode.Value != string.Empty)
{
Response.Redirect("~/Customer/Products.aspx?Search=" + trvCategoryTab.SelectedNode.Value);
}
}
It's working fine, but the problem is that the page reloads every time. I want to prevent this.
Is there any other method to redirect to the child page?
It sounds like you want to use AJAX.
Is there anyway to set a Master Page's Master Page programmaticly? Would I need to do this on each page? I don't have access to the pre_init event from Master. Hrm...
In the PreInit function place:
this.MasterPageFile = "~/masterPage.master";
Yes, you will need to put this on each page. To get aroud it, have all the pages inherit from a base Page and place put the master page reference there.
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
Working with ASP.NET Master Pages Programmatically