Run a script inside a content page. ASP.NET - asp.net

I have a masterpage and content page. And I'm trying to run a java script that needs to be executed on the page loading.
As I am using a master page do not have access to the field
My doubt is how to run the script within the content page? And where the script has to be? the head of the master page or inside the content page?

You can run C#/VB code on page load at any page or custom user control, so you will have access to whatever on the page.
I think you want to put that code in masterpage to standardize the procedure, one of the solutions (I don't know if you can accomblish this):
you can build your own "CustomPage" class by inheriting from "Page"
so, you can set the onLoadEvent to a preset action.
and inherit from it for all your pages.

You could put a ContentPlaceholder in the Head section of your master page. Thats if you want to put your script in the Head

Related

Dynamically add asp:content to a page

I have one problem. I want to setup master pages in my application. As I have too many aspx pages, I don't want to go to everypage and add asp:content tag in it. I want some dynamic way in which I can look into my page, copying it's html/other asp controls and adding it to dynamically created content template.
I know how to dynamically create content templates. But don't know whether it is possible copying aspx page's html and adding it to content template.
Let's say, In my aspx page's codebehind, on page_PreInit, I am adding following code.
base.AddContentTemplate("WebHeaderPH", new CompiledTemplateBuilder(new BuildTemplateMethod(this.getControl)));
getControl is a method which contain anything (html, text or may load a user control.)
This way, I am adding a content template dynamically.
Now my aspx page contents following code.
<span id="someid" runat="server">blah blah...</span>
Now what I want is, to get this span tag by it's id, and wrap it under asp:content. or may be in the above getControl method.
but putting this span on page, throw following error,
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Either I have to put, asp:content template on the page. but If I do this, then I don't need to create contentTemplate dynamically or keep whole page blank as I am creating contentTemplate dynamically. In both cases, I have to edit all my aspx pages.
Is there a way to get page's html and add it into contentTemplate without any need to edit aspx page?
I'm afraid you have to go step by step manually. It is indeed a design mistake, but it won't take you THAT long to change them all.
I've unfortunately made the same mistake as well and I fixed it by manually editing my pages.

Reload only Content Pages Inside the Masterpage

I have more than 100pages in my project & a single master page for all this. My masterpage has links to different pages. I want to change only the ContentPages( Child Pages) & my masterpage should not get reloaded.
For Example:
My Master page is : SiteMaster.master
Child Page1: Add.aspx
Child Page2: Sub.aspx
When I execute, Add.aspx comes inside the SiteMaster.master. Know, When I click the sub.aspx link inside the Add.aspx, only add.aspx page should be changed & sub.aspx must be loaded. I don't want to reload the master page.
If possible, please post some examples or links.
Your expected behavior is not exactly how master pages work. There may be ways to achieve a no postback solution in this scenario but the easiest one would be to use an <IFrame /> (which is usually frowned upon)
Master page is part of your pages. It's not loading separately.
Simple explanation:
ASP.NET engine takes your aspx and puts it inside the master page and then renders it as one page, then serves it to user.
If this is not what you want and you want that only content of your master page be loaded, then you should not use master pages at all! It's against nature of master pages. they act like skins for aspx pages.
Search for HTML IFrame tag and don't use master page.
P.S: IFrames are not used widely in this days.

Put Script Manager in master page

is it right to put script manager in master page ?
It isn't wrong, as such.
Really depends on your requirements.
If you need to use scripts in several pages that use the same master page, use ScriptManager in the master page.
Just keep in mind that you can only have one ScriptManager in the whole loaded page, so you can't add one in content pages as well.
See this article on MSDN for an example (Using the ASP.NET UpdatePanel Control with Master Pages).

Invoking javascript in child page from master page

Hii,
I have function established in child page, that is using in the childpages. I have to invoke the child page function from the master page. How it is possible in javascript
The master page concept only exists on the server side, when the page arrives at the browser it's just a single page.
So, you just call the function normally.
The two pages are merged into one at runtime, so you should have access to the Child page function from the Master page. Are you implementing the function differently depending on the child page? If not, the function might as well be included on the Matser Page.
You will have to make sure that each child page that uses this master page has this function. Otherwise the call from the master page will error. The easiest way to do that is to include a default ContentPlaceholder on the master page, with a version of the function that does nothing, and then override the contents in the child pages where appropriate. If a child page doesn't include a Content tag for that ComtentPlaceholder, the default script from the master page will be rendered.

ASP.NET master pages order of adding scripts

I have a master page that adds the jquery library via a registerclientscriptinclude:
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),
"JQuery",
Page.ResolveUrl("~/Scripts/jquery-1.2.6.min.js"));
In a page that uses that master page I want to include a script that depends on jquery:
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),
"jqplugin1",
Page.ResolveUrl("~/Scripts/jquery.plugin1.compressed.js"));
This causes a problem because the page's client scripts are written first and then the master pages client scripts (causing the plugin to complain that "jquery" is undefined).
Is there anyway to control the ordering of client script includes? Alternatively any recommendation on how best to control this (temporarily the master page is just including everything... but that's not going to work long term).
since master page is like a control embedded in a page, you can add these scripts towards the end of the page cycle, the control would fire first and then page, so your page would be fine.
You could re-include the jQuery library in the page, if it's already there it will simply override it, if not it will be added.

Resources