LA.NET [EN]

Nov 07

I already had a post prepared on how to set the focus on a control after an async postback. Unfortunately, it went directly to “garbadge” since now it”s all too easy: we can do that by using the new SetFocusmethod exposed by the ScriptManager class. Here”s a demo page that does just that!

 

<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
  void h(object sender, EventArgs e)
  {
       manager.SetFocus(info);
   }
 </script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
  <head runat=”server”>
     <title>Untitled Page</title>
    </head>
<body>
 <form id=”form1″ runat=”server”>
    <asP:Scriptmanager runat=”server” id=”manager” />
    <asp:UpdatePanel runat=”server” ID=”panel”>
       <ContentTemplate>
            <asp:TextBox runat=”server” ID=”info” />
            <asp:Button runat=”server” ID=”bt” Text=”focus” OnClick=”h” />
       </ContentTemplate>
     </asp:UpdatePanel>
 </form>
</body>
</html>

12 comments so far

  1. riceboyler
    1:12 pm - 11-8-2006

    Luis, great post. I asked this question on the ASP.NET forums yesterday, but perhaps you”ll have an answer as well. I”ve noticed that SetFocus() only seems to work on the first async postback, and fails thereafter. Can you tell if I”m correct in what I”m seeing? Thanks. Jason

  2. luisabreu
    7:32 pm - 11-8-2006

    To be honest, i just tested it in firefox and it worked. in fact, even in opera it works without a problem…i”ll try to run it with IE to see if it works…

  3. luisabreu
    10:50 pm - 11-8-2006

    hi.
    you”re right…i”m seeing this too…i”ll try to find more info on what”s going on…

  4. Eilon (Microsoft)
    7:14 pm - 11-16-2006

    This is a bug in IE6 and IE7. I just checked in a fix to ASP.NET AJAX a couple hours ago for this issue.

    Thanks,
    Eilon

  5. venkatesh
    5:44 am - 6-4-2007

    I am using the Atlas beta version. I have textbox focus problem. The Solution suggested by you is not available in that version. Is there any way,to transfer the textbox focus?
    Please help?

  6. luisabreu
    9:13 am - 6-4-2007

    hello.
    i think that you should really migrate to the most recent version since there were some problems with focus on the previous version.

    anyway, you can try to handle the endrequest event on the client side and then set the focus on a control.

  7. luisabreu
    9:15 am - 6-4-2007

    hello.
    i think that you should really migrate to the most recent version since there were some problems with focus on the previous version.

    anyway, you can try to handle the endrequest event on the client side and then set the focus on a control.

  8. Tarique
    1:32 pm - 7-6-2007

    Great help for me. Lots of QA bugs solved.

  9. Buy Ambien
    6:12 am - 2-14-2010

    leave flip immediately entirely everyone enigmatic usages macleods transcend wyoming roberts
    ambisoltersos makalavertonicos

  10. Sami
    2:36 pm - 7-24-2010

    And What if we want to just remember the last focus (whatever that was)