views:

227

answers:

3

The issue is now resovled :) Thanks for everyone's help and attention!

I'm getting the JS error "Unexpected call to method or property access" in IE6 intermittently on the line "oAutoCompleteTextBox.focus();". Hopefully, someone has seen this issue before and can provide some insight on how to solve it. Below is the context of the usage.

$(document).ready(function () {
    ...
    oAutoCompleteTextBox = GetElement('<%=this.AutoCompleteTextBox.ClientID%>');
    ...
    SetupDefaultValues();
}

function SetupDefaultValues() {
    ...
    if(canFocus(oAutoCompleteTextBox)) {
        oAutoCompleteTextBox.focus();
    }
}

My 1st post on stackoverflow - YAY!

A: 

Is oAutoCompleteTextBox declared globally? You're setting it in the document.ready function but trying to use it in another function.

Shawn Steward
Yes, oAutoCompleteTextBox is declared globally.
HOCA
A: 

are you sure its a textbox? what does "canFocus" function do? alert on that line, oAutoCompleteTextBox.tagName, then if it is "INPUT" alert .type, if it is "text" then you have problem :) knowing IE6, it might be a timing problem nothing but, if you call setupdefaultvalues in a settimeout of 10 seconds, i MIGHT work

Ayyash
I did verify that oAutoCompleteTextBox is in fact a TextBox (w/ tagName = INPUT). You maybe right about the timing problem, I also suspect that it may be timing related - particularly because the error is intermittent :(
HOCA
oh wait, is it readonly? or disabled?
Ayyash
It isn't readonly or disabled - but it doesn't have a parentElement... I wonder if this is related to the fact that it is sitting in a modal dialog extender.
HOCA
what does canFocus function do exactly?
Ayyash
canFocus() is just a method that determines in the control or any of its parent nodes are disable or visible = false.
HOCA
does that happen in IE7, IE8 or FireFox? i do remember in IE6 when playing with modal or modeless dialogs, it does lag... but my best guess in this stuation is: your using jquery onload, which is already faster than the usual window.onload, if you catch the body.onload of the modal dialog it might, just might work, but the best thing to do is: settimeout(fucn, 10) now i do remember that problem, but i dont work for IE6 anymore :)
Ayyash
A: 

OK, so the issue was that the jQuery $(document).ready() event isn't fired on updatepanel async postbacks. The solution is to refactor the function definition inside the ready() into an explicit function definition (i.e. function pageReady(){...}) and add the new pageReady() eventhandler to ASP.net Sys.WebForms.PageRequestManager endRequest event which is only fired on async postbacks.

So the code now looks like this:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(pageReady);
$(document).ready(pageReady);

function pageReady() {
    ...
    oAutoCompleteTextBox = GetElement('<%=this.AutoCompleteTextBox.ClientID%>');
    ...
    SetupDefaultValues();
}

function SetupDefaultValues() {
    ...
    if(canFocus(oAutoCompleteTextBox)) {
        oAutoCompleteTextBox.focus();
    }
}

Thanks for everyone's help and attention - took a while to figure out, I'm just glad it's resolved :)

HOCA
but all u did here is delay the onload for a few cycles, which what settimeout could have done... it is a timing issue isnt it?
Ayyash
As it turns out, timing wasn't the issue... The problem was that the pageReady() method was not being invoked because the jQuery $(document).ready() event is not fired during async postback inside an ASP.net UpdatePanel. Therefore the oAutoCompleteTextBox was not initialized properly. The change is to initialize the var oAutoCompleteTextBox on async postbacks using the Sys.WebForms.PageRequestManager endRequest event.
HOCA
but wait, if ready() wasnt being called how was it calling: SetupDefaultValues() and throwing an error? by the way, dialog boxes do not have a window object of their own, so if document.ready relies on window.onload, it is definitely not going to fire, when u open a dialog, do u pass the window object with it? i digress, does your solution work? move on :)
Ayyash
Sorry for the confusion, I simplified my example for the sake of brevity - SetupDefaultValues() is invoked from multiple places in the script. The solution does work. ;)
HOCA