Monday, October 29, 2007

Webform_AutoFocus() before Sys.Application.Initialize()?

Recently I was developing an ASP.NET Ajax extender control which added behaviors when the target control gained focus. It was a custom implementation of an auto-complete control. Picture your standard auto-complete control but with multiple columns. So if you were to enter customer organizations you could enter by a system id, customer name or city where the customer is located. You could even enter a combination these attributes.

Anyway, if you've worked with the ASP.NET Ajax ScriptControl before you would know that you wire up your events within the initialize method, which you override from the base JavaScript "class". This allows the web browser to complete rendering of the page so all controls are created and properly registered before your code tries to use them.

However, I ran in to a bit of a snag when I tried to use the ScriptManager.Focus() method to set the focus to my target control upon first rendering of the page. I have a user entry screen where users can enter many records in sequence. So basically the users enter data in a few fields, hits submit to add the record and then the focus returns back to the first field, ready for the user to continue entering data.

The idea seemed solid to me, but I found out that the focus behavior wasn't working as I expected. I took a look at the rendered page and noticed that the Webform_AutoFocus() call which is created by the ScriptManager.Focus() runs prior to the Sys.Application.Initialize() call which calls my controls initialize! This means that my focus event handler has not yet been wired up, and there is no way for me to listen to the focus event.

Why is this? I can't think of a reason why the framework would want the focus to occur before initialize. Doesn't the name initialize even seem to imply that it would be the first function to run?

So I thought of a potential workaround. What if I could interrogate the target control to see if it currently has focus when I run the initialize method? It's not the prettiest solution, but it seems like it has potential. Unfortunately this hit a bit of a snag as well. After some research it turned out that IE has a property on the document known as activeElement which will provide the control which currently has focus. The problem? Only IE has such a property to tell me what control has focus, and I hate doing something which only works in a single browser.

Unfortunately I wasn't able to find any other workarounds, and as such only IE will appear to handle the system derived auto focus correctly. I suppose there are worse things to worry about. The control still works without listening to the focus event, it just works a little differently than I would like.

Microsoft, why couldn't you have just put the auto focus after initialize?

--John Chapman

13 comments:

Anonymous said...

Hi JOHN,

I don't think so i understood one hundred percent what you are talking about. But to the knowledge i have, if you want a particular control to have focus by default after the page load completes in asp.net, you can use the property called DefaultFocus of form tag in your page. Set the DefaultFocus = Your Control ID and ASP.NET will take care about setting the focus.

Anonymous said...

Hi John,

Not sure if you're still interested in this one, but the following link may be of interest - it solved this problem for me:

http://forums.asp.net/t/1324475.aspx

priya said...


Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
Data Science Training in Indira nagar
Data Science Training in btm layout
Data Science Training in Kalyan nagar
Data Science training in Indira nagar
Data Science Training in Marathahalli
Data science training in bangalore

nisha said...

Every concept are explained very neatly. arranged every concepts are very unique.

Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery


EXCELR said...

I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.

EXCELR said...

I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community. data science courses

Jayalakshmi said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!

web designing training in chennai

web designing training in tambaram

digital marketing training in chennai

digital marketing training in tambaram

rpa training in chennai

rpa training in tambaram

tally training in chennai

tally training in tambaram

deiva said...

Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter
hardware and networking training in chennai

hardware and networking training in omr

xamarin training in chennai

xamarin training in omr

ios training in chennai

ios training in omr

iot training in chennai

iot training in omr

Unknown said...

I feel extremely appreciative that I read this. It is extremely useful and exceptionally enlightening and I truly took in a ton from it. data science training in kanpur

Maneesha said...

I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it..
business analytics course in hyderabad

Unknown said...

Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share. data science training in mysore

bharathpriya said...


Amazing blog. Great share of information. Very useful
how to learn matlab |computer science summer internships |iot online courses |number 1 summer and winter internship training and workshop service provider in india. |online c programming classes

|machine learning training | data science course fees in coimbatore |internship opportunities for engineering students |electronic engineering summer internships |wordpress training in chennai

Back linker said...

I'm totally playing your web website. You completely have a couple of willing intensity and charming stories. lovely and entirely lighthearted site on the web. Love to watch. keep up with Rocking. Spyhunter Registration Code

Blogger Syntax Highliter