Invoking javascript in child page from master page - asp.net

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.

Related

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.

Run a script inside a content page. 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

Stop Master page refreshing while navigating between pages?

I'm using Master Page in my ASP.net application, in the master page I put a ContentPlaceHolder in Update Panel to support AJAX in child pages, the question is how to stop Refreshing "master page controls" while navigating between pages?
For navigation between pages I tried to use Response.Redirect, windows.location java script with no success, shall I use the Frames or IFrames instead of Master Pages to stop Refreshing?
any suggestion to solve this issue will be highly appreciated, Thanks in advance...
If you don't want the page to refresh when switching between "pages", you will not have any good solution using master page. As others have said in different words, the master page is just a common "template" that is used by different pages. The navigation between is just like calling different pages, and of course will reload the entire page, including the master page content.
A sollution I have used with Ajax is
to have each "page" as a user
controls, and put them all in an
UpdatePanel with visible="false".
Then for navigation between "pages", switch
visibility for the user controls
to show the right "page" control.
The alternative is to use iframe.
Neither of these solutions use MasterPage.
The MasterPage concept was designed to simplify a common look before Ajax was introduced in ASP.NET. After Ajax became popular, the demand for not refreshing the entire page has been more common.
A masterpage is nothing more than extending your "normal" page with (most of the time) the default layout of your application. The master page and the contentplaceholders are rendered as a full html page. When you navigate between pages it is the normal behavior that your whole page refreshes. This is how the web works.
Working with an iframe could solve your problem. However that has some other side effects:
The whole masterpage isn't useful anymore. The content around your iframe is the "masterpage".
With a masterpage you actually browse to another url, you also see in the url bar of your browser. When you work with an iframe you navigate within the iframe to another page. The url in your browser will stay the same. When the user of your application hits the refresh button it always starts again at the default page you assigned to your iframe in the html. Of course there are some workarounds
Anyway. It really depends on your application. There are multiple solutions to work around the refresh behavior.
Having a structure like the one you've explained:
Master
Child page 1
Child page 2
...
Then you cannot prevent the page from refreshing when you switch from page 1 to page 2 etc. for you have a single "page" entity (master content + selected page content) when it's rendered to the browser.
If you want to switch betweent different app views inside the very same page (so to prevent a complete page refresh) you could use a single page (the Master becomes quite useless) with an updatePanel in which you load the different views.
You can also use iFrames, but if you have to handle any type of communication between different parts of the page (some of which are inside iFrames) I would personally advice not to use them.

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.

How does one call method exists in a page from another page?

I'm working on a page, in which other pages are opened from it in a modal way.
I need to call function exists in opener page to update certain controls in it.
I'm using window.open to open another window but in this case Page.PreviousPage for opened page is null.
I'm using
<%# PreviousPageType VirtualPath="~/PreviousPage.aspx" %>
to refer to Previous Page.
Any suggestions?
FYI: all aspx pages are AJAX-Enabled.
You can't call a method in the code behind of a Page class to update controls in a page that is displayed. The instance of the Page class only exists while the page is rendered, once the page is displayed in the browser, the Page object no longer exists.
The PreviousPage property is used when you make a post from one page to another, but it doesn't contain the Page object that was used to render the page, it contains a recreated Page object that will not be used to render anything at all. You can only use it to read information from the fields based on the information that was posted from it.
If you want to update the opener page you either have to do it on the client side using Javascript (), or reload the page so that the server side code can repopulate it. A combination of them both would be to use AJAX to update the page.
Edit:
You can for example use Javascript to access the opener and change the content of an element:
window.opener.document.getElementById('Info').innerHTML = 'updated';
You can also call a Javascript function in the opener page:
window.opener.doSomething('data');
That which gives you more possibilities, like making an AJAX call to load data from the server.
You can submit the parent page back to the server using javascript. You can use window.opener function in the javascript to access the parent page.

Resources