简体   繁体   中英

Call an ASP.NET function from javascript with a client side parameter

Actually the code is self-explanatory.. I am trying to assign a javascript variable the falue of a form element, which needs to be formatted by a function on the server..

the below code does not work. it does not get the form element but rather assumes $('#formvalue').val() is the string I am passing in..

var myvar = '<%= MyNamespace.MyClass.MyFunction("' + $('#formvalue').val() + '") %>'

any ideas?

You can use PageMethods to call a server function (public static WebMethod) from JavaScript.

Server-side function

[WebMethod]
public static void AbandonSession()
 {
   HttpContext.Current.Session.Abandon();
 }

JavaScript

<script language="javascript" type="text/javascript">
  //<![CDATA[
  function HandleClose() 
   {
     alert("Killing the session on the server!!");
      PageMethods.AbandonSession();
   }
   //]]>
</script>

Check this: http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide.all

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