Setting focus back to the control that caused the PostBack in an ASP.NET Form

SmartNavigation can be set to true on your ASP.NET webform so that when postbacks occur , the page when rendered back to the browser, will navigate back to the control that caused the postback.

But SmartNavigation can be problematic especially when dynamically loading controls onto your webform.

Therefore if you have SmartNavigation turned off = false, below is a piece of code that you can call from your webform  that will add javascript to your page, to automatically navigate back to the control that originally caused the postback.

I tested the code against IE6 and Netscape 7.1.

  ///


  /// This method will take passed webPage, and find the control that caused the postback. If it finds
  /// one it will set javascript on the page to set focus to that control
  ///

  /// The web page
  public void SetFocusPostBackControl(System.Web.UI.Page webPage)
  {
   string[] ctlPostBack;
   
   ctlPostBack = webPage.Page.Request.Form.GetValues("__EVENTTARGET");
   if (ctlPostBack != null && ctlPostBack.Length > 0)
   {
    string ctlUniqueId;
    ctlUniqueId = ctlPostBack[0];
    System.Web.UI.Control findControl = webPage.Page.FindControl(ctlUniqueId);
    if ((findControl != null) &&
     (findControl is DropDownList ||
     findControl is TextBox ||
     findControl is RadioButton ||
     findControl is RadioButtonList))
    {
     string ctlClientId;
     ctlClientId = findControl.ClientID;
     string jScript;
     jScript = "<SCRIPT language=\"javascript\"> document.getElementById('" + ctlClientId + "').focus(); document.getElementById('"
     + ctlClientId + "').scrollIntoView(true) </SCRIPT>";;
     
     webPage.Page.RegisterStartupScript("focus",jScript ); 

    }
   }
  } 

 

Comments

  • matt June 18, 2004 11:26 AM

    Outstanding. Just what I needed!
    Thanks.

  • matt June 23, 2004 6:25 PM

    You are an absolute legend. This is an incredibly useful little snippet of code.

    Thanks heaps.

  • matt July 28, 2004 7:22 AM

    Marvellous, it works great!

  • matt August 6, 2004 3:59 PM

    Thank you!!!!!!

  • matt November 19, 2004 1:01 AM

    Exactly what I needed, converted it to VB and got a "Request is not available in this context" error on the: webPage.Page.Request.Form.GetValues("__EVENTTARGET") line.
    Any ideas???

  • matt December 9, 2004 9:04 AM

    Not bad lad. Thanks.

  • matt December 15, 2004 12:03 PM

    Hi,

    it's goog thinking. it absolutely nice articles.

  • matt January 13, 2005 5:52 PM

    Matt,

    Is there an implementation of this that will work when a postback is caused by a cmd button? I may be showing my ignornace here, but the Request["__EVENTTARGET") is always null when posting back from a button.

    Thanks...
    Pam

  • matt January 14, 2005 8:53 AM

    Pam,
    Yes there is , But right now I cannot remember, all I can remember right now was a separate property on the form that could be used to determine what button was pressed. If I remember or find out I will post here.

    Matt.

  • matt January 14, 2005 6:50 PM

    Thanks! I'll check back periodically or post the answer if I find it elsewhere.

  • matt January 17, 2005 6:53 PM

    Function GetPostbackControl(ByVal targPage As Page) As Control
    If targPage.IsPostBack Then
    ' try to find the name of the postback control in the hidden
    ' __EVENTTARGET field
    Dim ctlName As String = targPage.Request.Form("__EVENTTARGET")
    ' if the string is not null, return the control with that name
    If ctlName.Trim().Length > 0 Then
    Return targPage.FindControl(ctlName)
    End If
    ' the trick above does not work if the postback is caused by standard
    ' buttons.
    ' In that case we retrieve the control the ASP-way: by looking in the
    ' Page's Form collection
    ' to find the name of a button control, that actually is the control
    ' that submitted the page
    Dim keyName As String
    For Each keyName In targPage.Request.Form
    Dim ctl As Control = targPage.FindControl(keyName)
    ' if a control named as this key exists,
    ' check whether it is a button - if it is, return it!
    If Not ctl Is Nothing Then
    If TypeOf ctl Is Button Then
    Return ctl
    End If
    End If
    Next
    End If

    Return Nothing
    End Function

  • matt February 1, 2005 3:56 PM

    I use this script and it works great, but as soon As I turn SmartNav on it takes me to the last control and not to where I want it, does anybody have any ideas ?

  • matt April 21, 2005 12:43 AM

    I would like to see something like this incorporated into future versions of VS.NET, except relying on a URL (WebForm1.aspx#controlname) not some JavaScript hack. If anyone is interested in commenting on such an issue, I posted a suggestion on MSDN Product Feedback at http://lab.msdn.microsoft.com/ProductFeedback/

    Suggestion ID: FDBK24578

  • matt June 6, 2005 1:49 PM

    Hi, thanks a lot for that nice piece of code! It also works in Firefox.
    Unlike your layout here ;o)
    Greetings from Berlin, Germany, Julian

  • matt August 16, 2005 4:58 PM

    If you're using ASP.NET 2.0, this piece of code works nicely in the PageLoad

    Dim ctlName As String = Me.Request.Form "__EVENTTARGET")
    If IsPostBack Then SetFocus(ctlName)

  • matt October 12, 2005 10:09 AM

    GREAT .... U SAVE US MANY HOURS OF THINKING OUT!!!

  • matt October 28, 2005 6:13 AM

    Hello
    We can also track on server side without using __EVENTTARGET which object raised event. I am not sure but I remember I read that some where. I am trying to find out that resource. Any body have idea about this?

  • matt January 25, 2006 3:33 AM

    Firstly Thank's alot
    but I could't reuse this in my webform please help !!!

  • matt January 25, 2006 8:18 AM

    What exactly is the problem ?

  • matt February 10, 2006 9:19 AM

    So how exactly do you call on this function from the webform?

  • matt April 14, 2006 2:55 PM

    hjkkklk

  • matt July 14, 2006 5:59 AM

    hello,

    where should i put the piece of code?

  • Nabeel Faruqui February 8, 2008 4:23 AM

    Outstanding !! yes it worked. Great piece of code.

    thanks very much

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS