Focus on my Survey project
Yesterday evening, I had some long debugging time because of a nasty Javascript function I was asking to add on on of my forms.
I wanted to do some total of few textboxes where users entered numbers, and display the result just below.
I did the server code for that, no problem at all using on Text Change event.
The problem is this is a part of a long questionnaire, and everytime the Server did a Postback, the form 'jump' back to the top of the page.
So because I knew the problem and how to fix it, I decided to include some Javascript to set the focus to the right box after my Postback.
This worked well, yes and no. It took me some time to understand that I have to put my client side function at the end of the form and not at the top using Page.RegisterStartupScript method.
Why ? Because my Forms are in a Control and not in the Page, so apparently if you have the Javascript at the start, and you want to dynamically link this to Control ID, it doesn't work because the Control is not still there.
Well this is my understanding, maybe I am wrong.
So I found this article on the excellent Dotnetjunkies , it's very clever and it really help me.
But I still have a problem. The Focus works very well, no problem but with a Javascript error. I don't bother for the moment, because my application need to run only with IE5.5 +, so the Javascript error stay on the corner of the browser.
But it's not really neat and I would like to know what's going on !
The Javascript error is Error: Object doen't support this property or method
and the Javascript code is:
<SCRIPT language='javascript'> document.getElementById('QD_D441').focus(); window.load(); </SCRIPT>
I used window.load() to refresh the page but maybe it's the wrong method ?!??