I have a master page and 30 child pages. In my master page, I am loading prototype js which is needed on my 29 child pages.
On 30th child page, I am having pivottable.js which loads C3 and D3 charts. PivotTable.js, C3.js, D3.js are having some conflicts with prototype.js and they are not working since the master page is having prototype js reference.
If I removed prototype.js reference from master page, pivot table works as expected but other 29 other pages which require prototype js are not working. :-(
Is there any way to remove prototype reference from master page on 30th child page alone before the scripts on 30th page loads?
Hoping a reply. Thanks in advance.
Put your JavaScript files inside ContentPlaceHolder like below in your master page.
//SCRIPTS THAT CAN LOAD ON ALL PAGES
<script type="text/javascript" src="..."></script>
<asp:ContentPlaceHolder runat="server" ID="critical_js_files">
//SCRIPTS THAT YOU DO NOT LOAD ON SOME CHILD PAGES
<script type="text/javascript" src="..."></script>
</asp:ContentPlaceHolder>
In the master page code behind, add this method:
public void DisableCriticalJavaScriptFiles()
{
critical_js_files.Visible = false;
}
And finally, add below code in your Page_Load method in your 30th child page or any page where you do not want load those js file:
protected void Page_Load(object sender, EventArgs e)
{
Master.DisableCriticalJavaScriptFiles();
//REST OF THE PAGE_LOAD CODE
}
Hope this helps.
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 }
I need to add two relative Jquery files path in my master pages Head section.
i try this for two file but just one of them calling in my "Head" section.i also use this cods in my master page Code Behind but it didn't work:
protected void Page_PreRender(object sender, EventArgs e)
{
string jquery = ResolveClientUrl("~/JQuery/jquery-1.4.4.min.js");
Page.ClientScript.RegisterClientScriptInclude("jquery", jquery);
string jqueryShadow = ResolveClientUrl("~/JQuery/jquery.shadow.js");
Page.ClientScript.RegisterClientScriptInclude("jqueryShadow", jqueryShadow);
}
Also ,when i try this:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
i got same error with 1 link!
When i define both of Jquery files in my master page Head section directly, every things works for me!how can i inject my Jquery files(more than one!) with relative paths exactly on the Head section in Master Page?
Any idea?
Regards.
Have you tried the solution proposed (by #Bharath) on the link you provided?
var control = new HtmlGenericControl("script") ;
control.Attributes.Add("type", "text/javascript");
control.Attributes.Add("src", Page.ResolveClientUrl("~/JQuery/jquery-1.4.4.min.js"));
this.Page.Header.Controls.Add(control);
You could put those statements on the Page_Load event of your MasterPage.
Regards,
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
Hi I have a usercontrol which includes some JavaScript, if I add the control to a standard web page I can start the JavaScript in the body tag, like this
<body onLoad="Start()">
The problem is that I need to add the control to a webpage which is inside a masterpage, how do I then start the script when a page inside a masterpage doesn't have a body tag.
You can register it using:
Page.ClientScript.RegisterStartupScript()
For your case, it would be this:
Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "Start()", true);
Include jQuery and put your initialization code inside $(document).ready().
If you want to strictly use .NET functionality, and your page is properly marked up with runat="server", you could use Page.ClientScript.RegisterStartupScript() as well.
You can change the body tag to runat server and give it an id.
<body id="masterBody" runat="server">
Then on page load:
public void Page_Load(Object sender, EventArgs e)
{
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("masterBody");
body.Attributes.Add("onload", "Start()");
}
Try binding to the DOM document.ready event to load your content after the document has completed rendering
Okay, so we all know about changing a master page dynamically in a page's OnPreInit event.
But what about a nested master page? Can I change a master's master?
There is no OnPreInit event exposed in the MasterPage class.
Any ideas?
Just tested this and it works from the PreInit of the Page that is using the nested MasterPage.
protected void Page_PreInit(object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}
Obviously you will need to ensure that the ContentPlaceholderIds are consistent across the pages you are swapping between.
We combine Andy's method with a "BasePage" class - we create a class that inherits from System.Web.UI.Page, and then all our pages inherit from this class.
Then, in our base page class, we can perform the relevant checks to see which root master page should be used - in our case we have a "Presentation" master, and an "Authoring" master - the presentation version has all the navigation and page furniture, along with heavy display CSS, while the authoring master has some extra JS for the authoring tools, lighter CSS, and no navigation (it's what we use when the user is actually authoring a page, rather than modifying the site layout).
This base page can then call Page.Master.MasterPageFile and set it to the Authoring master if that is the correct state for the page.
Just in case anyone stumbles across this and tears their hair out with a "Content controls have to be top-level controls in a content page or a nested master page that references a master page" error when trying Andy's code, get rid of the this.Master. So, the code becomes:
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "/Site2.Master";
}
Edit As Zhaph points out below, the code I have ^^ there will only change the current page's master, not the master's master. This is the code Hainesy was talking about when he mentioned "we all know about changing a master page dynamically" (which I didn't, d'oh). If you happen to get to this page by googling "stackoverflow change master page" (which is what I did) then this is possibly the code you're looking for :-)
To add on to the answer of Zhaph - Ben Duguid, (+1 by the way):
Here is example code that sets the master page of the nested master page. All pages inherit from this BasePage, so this code only exists in one place.
public class BasePage : System.Web.UI.Page
{
private void Page_PreInit(object sender, System.EventArgs e)
{
if (Request.Browser.IsMobileDevice)
{
if (Page.MasterPageFile == "~/master/nested.master"))
{
Page.Master.MasterPageFile = "~/master/mobile.master";
}
else
{
MasterPageFile = "~/master/mobile.master";
}
}
}
}