title: Maintain Scroll-Position on PostBack
body: <p>I use Visaul Studio Community 15.5.2 with WebForm and C#</p>
  
  <p>some suggest</p>
  
  <pre><code>&lt;%@ Page MaintainScrollPositionOnPostBack="true" .....
  </code></pre>
  
  <p>and on web.config</p>
  
  <pre><code> &lt;pages maintainScrollPositionOnPostBack="true" &gt;&lt;/pages&gt;
  </code></pre>
  
  <p>i tried, but the maintainScrollPositionOnPostBack has no effect, even without scripts, with no browser (Edge included)</p>
  
  <p>this code work with a only one DIV</p>
  
  <pre><code>&lt;script type="text/javascript"&gt; 
  window.onload = function () {
      var div = document.getElementById("dvScroll");
      var div_position = document.getElementById("div_position");
      var position = parseInt('&lt;%=!string.IsNullOrEmpty(Request.Form["div_position"]) ? Request.Form["div_position"] : "0" %&gt;');
          div.scrollTop = position;
          div.onscroll = function () {
              document.getElementById("div_position").value = div.scrollTop;
          };
  };
    &lt;/script&gt; 
  </code></pre>
  
  <p>the DIV with a HiddenField, everything works perfectly.</p>
  
  <pre><code> &lt;form id="form1" runat="server"&gt;
      &lt;div id="dvScroll"; style="width:450px; height:300px; overflow:scroll; float:left"&gt;
          &lt;asp:HiddenField ID="HiddenField1" runat="server" /&gt;
          &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CellPadding="4"&gt;
            &lt;Columns&gt;         
              &lt;asp:BoundField DataField="ID_NAVE" HeaderText="IdNave" ItemStyle-Width="50" ItemStyle-wrap="false" Visible="true" /&gt;
              &lt;asp:BoundField DataField="NOMENAVE" HeaderText="nave" ItemStyle-Width="15" ItemStyle-wrap="false" Visible="true" /&gt;
            &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
      &lt;/div&gt;
      &lt;input type="hidden" id="div_position" name="div_position" /&gt;
  </code></pre>
  
  <p>But with a second DIV the code is not applicable (window.onload=function() is unique, it would be nice to modify the script to accept the DIV as a parameter, but I don't know JS</p>
  
  <pre><code>&lt;div id="dvScroll2"; style="clear:both; width:600px; height:300px; overflow:scroll;"&gt;       
         &lt;asp:HiddenField ID="HiddenField2" runat="server" /&gt;
         &lt;asp:GridView ID="GridMnuRest" runat="server" AutoGenerateColumns="false" CellPadding="4" OnRowCreated="GridMnuRest_RowCreated" OnRowDataBound="GridMnuRest_RowDataBound" OnSelectedIndexChanged="GridMnuRest_SelectedIndexChanged" OnSelectedIndexChanging="GridMnuRest_SelectedIndexChanging" OnRowCommand="GridMnuRest_RowCommand"&gt;                            
             &lt;Columns&gt;       
              &lt;asp:TemplateField &gt;
                &lt;ItemTemplate&gt;
                  &lt;asp:CheckBox ID="MnuSelect" runat="server" AutoPostBack="true" OnCheckedChanged="GridMnuRest_OnCheckedChanged" /&gt;
                &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;                  
             &lt;asp:BoundField DataField="id_ristorante" HeaderText="IdRst" ItemStyle-Width="20" ItemStyle-wrap="false" Visible="true" /&gt;
             &lt;asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="300" ItemStyle-wrap="false" Visible="true" /&gt;
            &lt;/Columns&gt;
         &lt;/asp:GridView&gt;
     &lt;/div&gt;
     &lt;input type="hidden" id="div_position2" name="div_position2" /&gt;
  </code></pre>
  
  <p>even if I create a new script with changed variables</p>
  
  <pre><code>&lt;script type="text/javascript"&gt; 
  window.onload = function () {
      var div2 = document.getElementById("dvScroll2");
      var div_position2 = document.getElementById("div_position2");
      var position2 = parseInt('&lt;%=!string.IsNullOrEmpty(Request.Form["div_position2"]) ? Request.Form["div_position2"] : "0" %&gt;');
          div2.scrollTop = position2;
          div2.onscroll = function () {
              document.getElementById("div_position2").value = div2.scrollTop;
          };
  };
  </code></pre>
  
  <p></p>