Home / ASP.NET Wiki / Javascript / Set Focus Controls - AutoPostBack = true

Set Focus Controls - AutoPostBack = true

 Rate It (5)


Every time user’s complaints me about page scroll issues.
For example: If a lengthy page having a dropdown list with AutoPostBack = true at bottom of page. After the selection page gets reloaded and focus will be on top of the page. User needs to again scroll down the page for next key in. Here a sample solution for help... 

c# 

public void SetFocus(Page sPage)
    {
        string[] sCtrl = null;
        string sCtrlId = null;
        Control sCtrlFound = default(Control);
        string sCtrlClientId = null;
        string sScript = null;

        sCtrl = sPage.Request.Form.GetValues("__EVENTTARGET");
        if ((sCtrl != null))
        {
            sCtrlId = sCtrl[0];
            sCtrlFound = sPage.FindControl(sCtrlId);
            if ((sCtrlFound != null))
            {
                sCtrlClientId = sCtrlFound.ClientID;
 sScript = "<SCRIPT language='javascript'>document.getElementById('" + sCtrlClientId + "').focus(); if (document.getElementById('" + sCtrlClientId + "').scrollIntoView(false)) {document.getElementById('" + sCtrlClientId + "').scrollIntoView(true)} </SCRIPT>";
 sPage.ClientScript.RegisterStartupScript(typeof(string), "controlFocus", sScript);
            }
        }
    }

Call this function on page load

Example: SetFocus(this);

vb.net

 Public Sub SetFocus(ByRef sPage As Page)
        Dim sCtrl() As String
        Dim sCtrlId As String
        Dim sCtrlFound As Control
        Dim sCtrlClientId As String
        Dim sScript As String

        sCtrl = sPage.Request.Form.GetValues("__EVENTTARGET")
        If Not IsNothing(sCtrl) Then
            sCtrlId = sCtrl(0)
            sCtrlFound = sPage.FindControl(sCtrlId)
            If Not IsNothing(sCtrlFound) Then
                sCtrlClientId = sCtrlFound.ClientID
                sScript = "<SCRIPT language='javascript'>document.getElementById('" + sCtrlClientId + "').focus(); if (document.getElementById('" + sCtrlClientId + "').scrollIntoView(false)) {document.getElementById('" + sCtrlClientId + "').scrollIntoView(true)} </SCRIPT>"
                sPage.RegisterStartupScript("controlFocus", sScript)
            End If
        End If
    End Sub

Example: Me.SetFocus(Page)

This will set focus on last used dropdown list.

Revision number 4, Thursday, September 03, 2009 6:46:34 PM by suthish nair

Comments

Thank you this helped! However, I was having issues integrating it with MS Ajax and implemented the following solution effectively (with the control being inside an UpdatePanel) Protected Sub MySetFocus(ByVal p As Page) Dim strCtrl() As String = Nothing Dim strCtrlId As String = Nothing Dim strCtrlFound As Control = Nothing Dim strCtrlClientID As String = Nothing Dim strScript As String = Nothing strCtrl = p.Request.Form.GetValues("__EVENTTARGET") If strCtrl IsNot Nothing Then strCtrlId = strCtrl(0) strCtrlFound = p.FindControl(strCtrlId) If strCtrlFound IsNot Nothing Then Dim strID As String = strCtrlFound.ID strCtrlClientID = strCtrlFound.ClientID ScriptManager1.SetFocus(strCtrlClientID) End If End If End Sub -Aaron Hillyer, CITRMS

Nice technique. Thank you for submitting. Grady Christie.

very nice idea rtpHarry - Thanks

Harry, Thanks for spell correction :-)

what about calling Control.Focus() in the postback method?

Can't you just add this directive to the Page header? % Page Language="C#" ... MaintainScrollPositionOnPostback="true" %>

Related Articles

Bring the window.open() window on front

In asp.net application, new windows can be opened using window.open method either from the client side or from executing it from server side. See the list of parameters it takes http://msdn.microsoft.com/en-us/library/ms536651(v=vs.85).aspx The problem most

Preprocessor Directives - Design Practice

What is a Preprocessor Directive? A preprocessor directive isa piece of code that is meant explicitly for the compiler. This offers a programmer to focus compilation for a specific environment at compile time instead of runtime. How do i identify a Preprocessor

ASP Image Control Enchanced

ASP Image Control Enchanced with help of ScriptControl and javascript.When I've started working as a ASP.NET webdeveloper I was troubled by the limitations of HTML. One of such limitation that emerged as a problem later in my work was how to correctly

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. proffy (1)
  2. primillo (1)