Sys.WebForms.PageRequestManager pageLoaded event caching function? - c#

I've got a master page that contains this code at the bottom of the page:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(parseData);
</script>
</form>
the parseData() function I'm creating inside the main page and I'm adding functions to it based on each page load. parseData IS called after every AJAX refresh, but it appears to only call the contents of the function from BEFORE the request. If I hit F5 to refresh the page again, it will properly call all of the newly added content in the parseData function.
Does this function cache that data? How can I make sure it calls the newly created contents of the parseData function?

According to the research I did, this is a problem with MS AJAX UpdatePanels and you are really better off not using them. I revised my JavaScript to pull data from hidden form fields instead so the JavaScript functions never changed and was able to get this working.

Related

Call view javascript function from javascript file

I have a jquery template that is being loaded with values after the ajax call is done and not on the view load.
on page load my javascript calls and function from a script.js file. i need that script.js file to call another function from the view. How would i call the listload function from the script.js
var limit = 5, dir = ' Desc', sort = 'Created', index = 0, autoscroll = false;
function getDataUrl(index, action) {
return '/Team/Linking/ListItemLinks/#Model.ItemId/?type=#Model.ItemType';
}
function listload() {
alert('test');
}
Corey,
As per my understanding, you will invoke a function in a js file from a view and after that the js file will invokes another function residing in the same view.
I think you can achieve it (By theoritically) because,
When the page loads, it will clubs all the js.
Consider, if your js file points to a Master/layout page , when the inner page loads it will have the cumulative js (from page,outside js files and master/layout pages). So you can call the function residing in the page from js (need to verify).
I think jQuery may have similar kind of stuffs, because we are calling jquery using an external file, but after that it calls the next line.
Another thing is to check with the callbacks and events in javascript. I am not expert in
topic. You can check the following links, may be it helps you.
Create a custom callback in JavaScript
http://blog.pengoworks.com/index.cfm/2012/1/12/Adding-custom-callbacks-to-existing-JavaScript-functions

How to execute jquery script before page load?

I need to execute javascript before Page load in ASP.NET application.
My function returns user location, and I would like to pass value to server side and load data based on location.
The problem is javascript function executes after page load.
Here is my code:
$(function () {
getLocation();
});
in getLocation function I set hidden field value
$("#<%= HfLocation.ClientID %>").val(location);
in code behind I try to get value but it's always empty
protected void Page_Load(object sender, EventArgs e)
{
var location = HfLocation.Value;
}
I need to execute javascript before Page load in ASP.NET application
This requirements makes no sense. Remember how ASP.NET works:
A user request hits the web server
The web server dispatches the request to ASP.NET engine.
The ASP.NET engine instantiates the page and goes through the entire page lifecycle.
The page is rendered as HTML and is sent to the client
The client browser builds the DOM, runs client side javascript, ...
You see that it is impossible to have step 5 execute before step 3 (in which the Page_Load event executes).
I need to execute javascript before Page load in ASP.NET application.
In that case, you will need to make two page requests. You can't do it with a single page request, since Page_Load() runs before the HTML+JS is even sent to the client.
Instead, have a mini-page that makes the JS call and then either does a postback to the current page, or (probably better), loads a new page. You can pass the location data either through a regular (non-server) form, or perhaps by setting a cookie from JS on the client, or encode it into the query string.
The second page/request will then have the data you need when it's Page_Load() event fires.
Do an ajax callback using javascript to request the data.

During PostBack JavaScript losing its functionality

I have one GridView which has one CheckBox and three TextBoxes in its template column.
The logic is, when I check the CheckBox, the corresponding TextBoxes should get enabled. If I uncheck the CheckBox, then the corresponding TextBoxes should get disabled. I have written JavaScript for this functionality.
Everything is working fine, but in that page I have one DropDownList too. When I change the DropDownList value, the page gets PostBack and at that time I lost the JavaScript functionality, i.e. the enabled TextBoxes in the GridView gets disabled. How to solve this problem?
After every post back in the update panel you need to reinitialize your javascript, because as you understand the struct of the html has change and javascript runs on the previous one that not exist after the update panel updates. Microsoft gives a functionality to do that as follow.
This is javascript lines.
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
// here initialize again your javascript
}
</script>
Use the RowCreated event for the ASP Grid to add the client function on the check box, this way the client function will stay on each postback
You must register your javascript at each postback for it to run, by using the ScriptManager.RegisterStartupScript method: here's an example (edited for article in english, sorry :p).
Use auto post back with check box to enable disable text box from server side code. This will update the view state and so it will be maintained on select change. Use these inside UpdatePanel for ajax. Alternatively, you can add drop down into updatepanel as well, so that select change does not affect other page elements.
JavaScript is working on DOM. When page gets PostBack values from ViewState are written to DOM and overwrites your JavaScript changes. You should store state of checkboxes in persistent way ie. jQuery data() or hiddenfield and after PostBack write changes back to DOM.

Expire a particular page on back button of browser

if users press the browser's back button to reach the prior page, the page should display a message like "web page expired".
can i use javascript for this???
for example:
there are 4 pages in web sites. on page 1,2 and 3 the user can use the back-button, wheras on the 4th page the user gets the desired message.
i thought that i can do this by using counter.
i used following javascript on the master page ..
<script type="text/javascript">
function GoBack() {
window.history.go(+1);
}
</script>
and call the function in body like this:
<body onload="GoBack();">
and on the 4th page_load i do the following:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
it is working for all pages .. but i want to do this only for the 4th page
If you only want it on that page level, and when you use postbacks, then I suggest you simply keep it in ViewState instead of Session state. Session's also still available on other pages, where you might want to have other counters.
You need to keep the variable alive across requests. So one way is to put it in some viewstate or sessionstate. Sessionstate is least preferred. But you can possibly put it in a hidden textbox in the page and simply use it.
Looking at the problem after the much awaited update/edit, I shall suggest you to use SessionState. Please give a try on it.

Run javascript function after Server-Side validation is complete

Ok, I've got a lightbox with a small form (2 fields) in it, inside an UpdatePanel, and I want to close this lightbox (must be done via javascript) when the 'Save' button is pressed.
However, there is a need to have a server-side CustomValidator on the page, and I only want to close the lightbox if this returns as valid.
Does anyone know a way to trigger javascript (or jQuery) code from a server-side validator?
You can add a little snippet of code using the ScriptManager to execute after the response comes back to the UpdatePanel.
if (Page.IsValid){
ScriptManager.RegisterStartupScript(
customValidator1,
typeof(MyPageClass),
"closeBox",
"myLightBoxVariableOnThePage.close()",
true);
}
When that server side validator runs, it will send a whole new page to the browser. Anything that was shown in the browser before was destroyed, including any state kept in your javascript. If new page bears a strong resemblance to the old page, you should consider this a happy coincidence.
Therefore, the thing to do here is rather than executing a javascript function, have your CustomValidator make the correct changes to the page on success so that it's rendered to the browser correctly in the first place.

Categories