Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Wednesday, March 28, 2012

pageLoad and pageUnload called when using PageMethods ?

I have pageLoad and pageUnload functions in my page where I do my initialization and cleanup. I also use UpdatePanels and PageMethods. I am noticing that pageLoad and pageUnload methods are getting called on every PageMethod (ajax) call !!.

Doesn't pageLoad and pageUnload correspond to window.load and window.unload events respectively ?

Basically, I want methods which should be called when _entire_ page is loaded or unloaded and should not be called while using PageMethods or postbacks from within UpdatePanel

Please guide.

Regards & thanks

Kapil


Hi ksachdeva17,

I hope i understand your situation correctly. You don't want that the page life cycle will be invoked when there is an event triggered by an action from a control inside your updatepanel (or trigger). Well the situation on the server will be the same when you're using an updatepanel. The async postback will take the same cycle as an synchronous postback. The thing that is different is the rendering of the page. The controls in your updatepanel will only be rendered and not the whole page. I hope this will make things clear for you.

Regards,


Thanks Dennis,

You are saying that window.onload and window.unload (DOM) events will occur if we make XMLHttprequest from the page, I would guess they should not as XMLHttpRequest is independent of browser post !!. I understand that on server side async postback would take the same cycle as synchronous postback but on the page (in client browser) it should not.

Regards

Kapil


Hi Kapil,

I was saying that the page life cycle on the server will stay the same no matter if it's a synchronous of asynchronous postback. I found this in the documentation of Ajax.

Client Page Life-cycle Events

During ordinary page processing in the browser, thewindow.onload DOM event is raised when the page first loads. Similarly, thewindow.onunload DOM event is raised when the page is refreshed or when the user moves away from the page.

However, these events are not raised during asynchronous postbacks. To help you manage these types of events for asynchronous postbacks, the PageRequestManager class exposes a set of events. These resemblewindow.load and other DOM events, but they also occur during asynchronous postbacks. For each asynchronous postback, all page events in the PageRequestManager class are raised and any attached event handlers are called.

Hope this helps!

Regards,


Thanks Dennis,

So my inference is that pageLoad is not same as window.onload. If you could please verify my understanding. I think I should be able to add the event handler using $addHandler with 'load' as an event name to execute things when page is loaded for the first time (no async) . Correct ?

Regards

Kapil


HI Kapil,

PageLoad and window.load are not the same indeed. you can handle also the logic you want in you code behind using thefollowing funciton:

if

(!ScriptManager.GetCurrent(this).IsInAsyncPostBack)

{

myTextBox.Text =

"Test";

}

Probably you already see what it's doing. It will check if it's in a asynchronous postback, when you want to handle some functionality when the page is in a synchronous postback you can you the statement above.

It is also possible to add a handeler to the load event of your window! like you suggested

Good luck

Regards,

Wednesday, March 21, 2012

parseLocale ignores negative currency format

I have some JScript that is utilized by a custom TextBox control that does various validation and format functions on the client side. In particular, when the control receives focus I left-justify the value in the textbox and format it without currency and grouping characters. Just the raw number. Then when the control loses focus (blur), I re-format the contents based on a property that indicates which formatting to use. This custom TextBox accepts only numeric input. In the case of a curreny input (DisplayFormat = "C") and a negative number, the number is formatted using the current culture's CurrencyNegativePattern of CultureInfo.NumberFormat. AJAX handles all of this for me since I call the Number.localeFormat function. The problem is that when the TextBox receives focus, I try to strip all formatting to get the raw number but for a negative currency value AJAX'sparseLocale function does not recognize the parantheses that are added to the number when it is formatted bylocaleFormat. Example: -1000 is formatted as ($1,000.00) with a format string of "C2" but when you pass this toparseLocale you get "1000.00". Shouldn'tparseLocale recognize the negative patterns its counterpartlocaleFormat adds to the value?

function numberEditorFocus(src)
{
src.style.textAlign ='left';
var displayFormat ="D";
if (src.value !="")
{
var val = Number.parseLocale(stripCurrency(src.value));
alert(val);
if (isNaN(val) ==false)
{
var newValue = val.localeFormat(displayFormat);
src.value = newValue;
}
src.select();
}
}

Sounds like it may be a bug, take a look in the source code for the AJAX Framework or theissues list

-Damien


Nothing in the bug list. It's obvious from the source code in the AJAX library that the parseLocale (and subsequently the parse) function does not consider the negative currency pattern. It only accounts for the preceding +/- sign. So, is this a bug or is there some reason it was designed with way? AJAX team?

Number.parseLocale = function Number$parseLocale(value) { /// <param name="value" type="String"></param> /// <returns type="Number"></returns> var e = Function._validateParams(arguments, [ {name: "value", type: String} ]); if (e) throw e; return Number._parse(value, Sys.CultureInfo.CurrentCulture);}Number.parseInvariant = function Number$parseInvariant(value) { /// <param name="value" type="String"></param> /// <returns type="Number"></returns> var e = Function._validateParams(arguments, [ {name: "value", type: String} ]); if (e) throw e; return Number._parse(value, Sys.CultureInfo.InvariantCulture);}Number._parse = function Number$_parse(value, cultureInfo) { var valueStr = value.trim(); if (valueStr.match(/infinity/i) !== null) { return parseFloat(valueStr); } if (valueStr.match(/^0x[a-f0-9]+$/i) !== null) { return parseInt(valueStr); } var numFormat = cultureInfo.numberFormat; var decSeparator = numFormat.NumberDecimalSeparator; var grpSeparator = numFormat.NumberGroupSeparator; var numberFormatRegex = new RegExp("^[+-]?[\\d\\" + grpSeparator + "]*\\" + decSeparator + "?\\d*([eE][+-]?\\d+)?$"); if (!valueStr.match(numberFormatRegex)) { return Number.NaN; } valueStr = valueStr.split(grpSeparator).join(""); valueStr = valueStr.replace(decSeparator, "."); return parseFloat(valueStr);}


I'd modify the source and recompile for now so that it works for you, and I would log this as a bug...

-Damien