简体   繁体   中英

ASP.Net Ajax PageMethod - retain reference to DOM object

When calling an ASP.Net PageMethod, we call it as follows:

function doSomething(htmlElement)
{    
     PageMethods.GetText(onSuccess, onFailure);
}

What is the best way to retain a reference to the htmlElement in the above example, so that we may continue to work with it in the onsuccess method?

Thanks for any help in advance

Due to the fact that Javascript supports closures , you won't need to worry about maintaining the reference to the element; since it's lexically scoped within onSuccess(assuming you're inlining an unnamed function in the place of onSuccess.)

Simply put, the function you put in for onSuccess can already use the reference to the element as if it were passed in as a parameter.

function doSomething(htmlElement)
{         
    PageMethods.GetText(function(x, y){ var v = htmlElement /*won't be null*/   } , onFailure);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM