tags:

views:

835

answers:

5

I used this code to maintain scroll position and don't have a clue of what it means. If someone has the time, can you provide me with an step by step explanation of what it is doing. Here it is:

<script language="javascript"  type="text/javascript">
    var xPos, yPos;
    var prm = Sys.WebForms.PageRequestManager.getInstance();

        function BeginRequestHandler(sender, args) {

        if ($get('<%=lstAuctions.ClientID %>') != null) {

            xPos = $get('<%=lstAuctions.ClientID %>').scrollLeft;
            yPos = $get('<%=lstAuctions.ClientID %>').scrollTop;
        }
    }

    function EndRequestHandler(sender, args) {

        if ($get('<%=lstAuctions.ClientID %>') != null) {

            $get('<%=lstAuctions.ClientID %>').scrollLeft = xPos;
            $get('<%=lstAuctions.ClientID %>').scrollTop = yPos;
        }
    }

    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);

</script>
+6  A: 
Tomh
Very nice, thanks! What exactly is the beginRequest and endRequest? Is this similar to the Page_Load and Page_Unload in asp.net?
Xaisoft
From MSDN, I never used it personally.beginRequest = Raised before processing of an asynchronous postback starts and the postback request is sent to the server.endRequest = Raised after an asynchronous postback is finished and control has been returned to the browser.
Tomh
beginRequest and endRequest are (likely) DOM-based custom events. The prm object has some easy syntax to assign your function to be called. Based on the naming, beginRequest and endRequest sound like pre- and post- function calls in some AJAX request.
jro
+4  A: 

Sys.WebForms.PageRequestManager is an ASP.Net AJAX construct.

Specifically in your code, there are some allocated variables (xPos,yPos,prm) and two defined functions (BeginRequestHandler,EndRequestHandler). At the end of the code are two function calls (prm.add_beginRequest,prm.add_endRequest) that are assigning those functions as event handlers.

The $get calls are part of the library as a shortcut for getting data from the client-side. It's very much javascript under the covers, but it's just a syntactical implementation through the ASP.Net AJAX client-side library.

jro
So instead of $get, what could I have used?
Xaisoft
Use $get because the .net generated ID's are only known after page generation. With less crappy implementations of generated code (I really hate asp.net for those ID's) you can use document.getElementById(<id here>);
Tomh
ok, so I can either use $get or document.getElementById(). $get is just an microsoft shortcut, is this correct?
Xaisoft
yes, $get() appears to be a shortcut to document.getElementById(). Pay heed to Tomh comment about generated Ids on the client-side.
jro
$get is not a shortcut, .net generates ids like ctl_001_001_originalId if you use originalId as id in your aspx page. You do not know this prefix, thats why MS thought it was handy to build a function which does know that prefix so you can just use the id you assigned and the function adds the rest.
Tomh
ok TomH, you are referring to naming containers, right?
Xaisoft
tomH, are you sure $get() is not a shortcut? In this line of code:$get('<%=lstAuctions.ClientID %>')the clientID value will be rendered correctly at runtime, but I believe $get is written to the page. Thus, it should map to javascript routine in the JS client libs.
jro
+1  A: 

var xPos, yPos;

**declares two global variables.


function BeginRequestHandler(sender, args) {

**declares a new function. This function is probably used for an event handler

if ($get('<%=lstAuctions.ClientID %>') != null) {

**this is a combination of inline ASP/ASP.NET code as defined in the <% %> pairing.

xPos = $get('<%=lstAuctions.ClientID %>').scrollLeft;

**captures the current scrolling position of the page into the local variable.

yPos = $get('<%=lstAuctions.ClientID %>').scrollTop;

**captures the current scrolling position of the page into the local variable.


function EndRequestHandler(sender, args) {

**declares a new function. This function is probably used for an event handler

if ($get('<%=lstAuctions.ClientID %>') != null) {

**this is a combination of inline ASP/ASP.NET code as defined in the <% %> pairing.

$get('<%=lstAuctions.ClientID %>').scrollLeft = xPos;

**sets the scrolling position of the page to the value of xPos.

$get('<%=lstAuctions.ClientID %>').scrollTop = yPos;

**sets the scrolling position of the page to the value of xPos.


var prm = Sys.WebForms.PageRequestManager.getInstance();

**declares and initializes a new variable to the PageRequestManager.

prm.add_beginRequest(BeginRequestHandler);

**adds the event handler defined above to the beginRequest of the current page.

prm.add_endRequest(EndRequestHandler);

**adds the event handler defined above to the endRequest of the current page.

+2  A: 

You did ask...

// declare 2 variables
var xPos, yPos;
// get an instance of the PageRequestManager - this looks like an MS ajax helper class
var prm = Sys.WebForms.PageRequestManager.getInstance();

// declare a function
function BeginRequestHandler(sender, args) {

    // get the ClientSide HTML DOM element which corresponds to the lstAuctions asp control on the serverside
    if ($get('<%=lstAuctions.ClientID %>') != null) {
        // if the element is not null (eg: page is not broken)

        // get the x Position of the object relative to what is displayed by the scrolled window (if you scroll sideways this value changes)
        xPos = $get('<%=lstAuctions.ClientID %>').scrollLeft;
        // get the y Position of the object relative to what is displayed by the scrolled window (if you scroll up/down this value changes)
        yPos = $get('<%=lstAuctions.ClientID %>').scrollTop;
    }
}

// declare a function
function EndRequestHandler(sender, args) {

    // get the ClientSide HTML DOM element which corresponds to the lstAuctions asp control on the serverside
    if ($get('<%=lstAuctions.ClientID %>') != null) {
        // if the element is not null (eg: page is not broken)

        // set the x position of the object to what we got last time (horizontal scroll the page)
        $get('<%=lstAuctions.ClientID %>').scrollLeft = xPos;

        // set the y position of the object to what we got last time (vertical scroll the page)
        $get('<%=lstAuctions.ClientID %>').scrollTop = yPos;
    }
}

// tell the page request manager to call our BeginRequestHandler method when it begins it's request
prm.add_beginRequest(BeginRequestHandler);

// tell the page request manager to call our EndRequestHandler method when it ends it's request
prm.add_endRequest(EndRequestHandler);

Basically, it looks like the page is using the MS ajax library to display some dynamic content (probably replacing a list with another list), but preserving the place that the user has scrolled to so the page doesn't "jump" when the new content replaces the old content.

Orion Edwards
Thanks, what is the $get for and can the BeginRequestHandler and EndRequestHandler be named anything?
Xaisoft