Setting Control Focus when AJAX is involved

April 16th, 2008

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.

Chris Brandsma Esoterica

  1. Deepa
    August 14th, 2008 at 11:50 | #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. November 20th, 2008 at 05:17 | #2

    It works wonderful

  3. Ramakrishna Vajrala
    December 16th, 2008 at 09:21 | #3

    works well ..

Comments are closed.