Note that there are some explanatory texts on larger screens.

plurals
  1. POTo get focus back after postback. ASP.Net MVC 2 Web application using multiple html forms
    text
    copied!<p>I am using ASP.NET MVC2 application in C#. I have two HTML forms on my content page.</p> <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt;&lt;%: ViewData["Message"] %&gt;&lt;/h2&gt; &lt;form id="form1"&gt; &lt;input id="text1" type="text" /&gt; &lt;input type="submit" value="submit" /&gt; &lt;/form&gt; &lt;form id="form2"&gt; &lt;input id="text2" type="text" /&gt; &lt;input type="submit" value="submit" /&gt; &lt;/form&gt; &lt;/asp:Content&gt; </code></pre> <p>What I want to do is to set the focus to Text Box after post back. After "form1" is submitted, "text1" should have focus and similarly after "form2" is submitted, "text2" should have the focus so that the user doesn't have to use mouse to put the focus back in text box and continue typing.</p> <p>The following code works fine for a single HTML form on the page.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; void function setfocus() { document.getElementById("text1").focus(); } &lt;/script&gt; &lt;/head&gt; &lt;body onload="setfocus();"&gt; &lt;form method="post" action=""&gt; &lt;input type="submit" value="submit"&gt; &lt;input id="text1" type="text"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>*The problem is I have two HTML forms on the page. Another issue is my web forms are on the content page of asp.net master page which reside inside the and only master form has tag which has "onload" property.</p> <p>A friend of mine suggested me to use Ajax's ScriptManager and UpdatePannel. However, the UpdatePannel doesn't like HTML's elements inside it. It seems only support asp.net control elements.</p> <p>Very grateful for your guidance.</p>
 

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