Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent the Focus() method from scrolling the page to the top
    primarykey
    data
    text
    <p>I have set of textboxes in a gridview and I use the <code>Focus()</code> method to restore the focus after losing to the intended text box. The problem is :</p> <p>The page (scrollable) and when I call the Focus method, in the text changed event, the page jump to the top. It's such a confusing behavior.</p> <p>My question is:</p> <p>Is there some way to prevent the <code>Focus()</code> method from jumping the page to the top?</p> <p>My code:</p> <pre><code>protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e) { calc(); int index = ((System.Web.UI.WebControls.GridViewRow)(((RadTextBox)sender).Parent.NamingContainer)).DataItemIndex; ((RadTextBox)gv_Evaluation.Rows[index + 1].Cells[3].FindControl("txt_evaluateWeights")).Focus();//Here is the problem. } </code></pre> <p><strong>Note:</strong> </p> <ul> <li><p>I use the <code>asp:TextBox</code>, and the same problem.</p></li> <li><p>My grid view in an update panel</p></li> </ul> <hr> <p><strong>EDIT :</strong></p> <p><strong>Javascript workaround:</strong></p> <pre><code>var lastFocusedControlId = ""; function focusHandler(e) { document.activeElement = e.originalTarget; } function appInit() { if (typeof (window.addEventListener) !== "undefined") { window.addEventListener("focus", focusHandler, true); } Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler); Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler); } function pageLoadingHandler(sender, args) { lastFocusedControlId = typeof (document.activeElement) === "undefined" ? "" : document.activeElement.id; } function focusControl(targetControl) { if (Sys.Browser.agent === Sys.Browser.InternetExplorer) { var focusTarget = targetControl; if (focusTarget &amp;&amp; (typeof (focusTarget.contentEditable) !== "undefined")) { oldContentEditableSetting = focusTarget.contentEditable; focusTarget.contentEditable = false; } else { focusTarget = null; } try { targetControl.focus(); if (focusTarget) { focusTarget.contentEditable = oldContentEditableSetting; } } catch (err) { } } else { targetControl.focus(); } } function pageLoadedHandler(sender, args) { if (typeof (lastFocusedControlId) !== "undefined" &amp;&amp; lastFocusedControlId != "") { var newFocused = $get(lastFocusedControlId); if (newFocused) { focusControl(newFocused); } } } Sys.Application.add_init(appInit); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload