Setting the focus on a control after an async postback

Published Tue, Nov 7 2006 16:28

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>

Filed under: ,

Comments

# riceboyler said on Wednesday, November 08, 2006 7:12 AM

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

# luisabreu said on Wednesday, November 08, 2006 1:32 PM

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...

# luisabreu said on Wednesday, November 08, 2006 4:50 PM

hi.

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

# Eilon (Microsoft) said on Thursday, November 16, 2006 1:14 PM

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

# venkatesh said on Monday, June 04, 2007 12:44 AM

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?

# luisabreu said on Monday, June 04, 2007 4:13 AM

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.

# luisabreu said on Monday, June 04, 2007 4:15 AM

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.

# Tarique said on Friday, July 06, 2007 8:32 AM

Great help for me. Lots of QA bugs solved.

Leave a Comment

(required) 
(required) 
(optional)
(required)