title: Begin/EndRequestHandler multiple handlers in custom ASP.NET controls
body: <p>I have a custom ASP.NET control, a dropdown/treeview, which needs to add Begin(End-)RequestHandlers to prevent scrolling to the top of container during UpdatePanel partial postbacks (as described <a href="https://stackoverflow.com/questions/5288682/maintain-panel-scroll-position-on-partial-postback-asp-net">here</a>). They are added dynamically like so:</p>
  
  <pre><code>Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, string.Format(@"
                    var xPos, yPos;
                    var prm = Sys.WebForms.PageRequestManager.getInstance();
  
                    function BeginRequestHandler(sender, args) {{
                      if ($get('{0}') != null) {{
                        // Get X and Y positions of scrollbar before the partial postback
                        xPos = $get('{0}').scrollLeft;
                        yPos = $get('{0}').scrollTop;
                      }}
                   }}
  
                   function EndRequestHandler(sender, args) {{
                       if ($get('{0}') != null) {{
                         // Set X and Y positions back to the scrollbar
                         // after partial postback
                         $get('{0}').scrollLeft = xPos;
                         $get('{0}').scrollTop = yPos;
                       }}
                   }}
  
                   prm.add_beginRequest(BeginRequestHandler);
                   prm.add_endRequest(EndRequestHandler);  ", this.ItemsContainer.ClientID), true);
  </code></pre>
  
  <p>The problem begins when I add more than one instance of the control to the page. Both scripts are rendered and registered BUT in the end only one, the last one on the page, ends up attached to the the containers, ie. wherever there is update in panel X or Y, only the JS for panel Y is executed - twice!</p>
  
  <p>A better option would surely be appending just one pair of Begin/End handlers, which I could perform by, for example, adding a static key for the RegisterStartupScript method. The only problem is that I would need to pass a parameter of the panel for currently updating UpdatePanel.<br/><br/>
  Any idea how to do this, and combine with the above?</p>