Setting Control Focus when AJAX is involved

Here is an annoying problem I just ran into.

I have a web form that has been working just fine for a long time…until I introduced it to the Microsoft Ajax library and starting doing some partial loading.  Nothing overly fancy, just using the value of one control to load values into another control.

Problem is that I lost the control focus position.  That is kind of key for keyboard users (or laptop users with crappy touch-pad mice to work with).

So the normal fix is to use Page.SetFocus.  Doesn’t work in Ajax land for some reason.  But there is a workaround.

ScriptManager.GetCurrent(Page).SetFocus(Control);

 

If you are using the Microsoft Ajax Library you are forced to use a ScriptManager control (or a ScriptManagerProxy for you MasterPage users).  From your page, ScriptManager.GetCurrent is actually a static function, so you can still use it if you page only has a ScriptManagerProxy.

Not to say this is foolproof.  It is setting a control’s focus…but always the right control.  Still looking into that.

4 thoughts on “Setting Control Focus when AJAX is involved

  1. HI,
    I am new to .net and Ajax.
    Here is the problem,

    We have in masterpage
    and content page(aspx) has the update panel, inside update panel we are calling ascx control.
    In the ascx page we have dropdown listbox. On selected indexchange of dropdown, the focus got lost. I would like to put the cursor back after postback.

    Please, help me on this.

  2. IT works for first two times and then it gets stopped please suggest me a solution, i have 10 textboxes on my page but after 2 textboxes its stopped working

Comments are closed.

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.