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 here). They are added dynamically like so:
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);
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!
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.
Any idea how to do this, and combine with the above?