Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To send a value from javascript to the server you have a couple of options:</p> <ol> <li>Use AJAX if you want to stay on the current page</li> <li>Redirect to a server side script and pass the value in the querystring</li> </ol> <p>Let's consider the first case:</p> <p>Assuming you have a web method capable of receiving the value on the server:</p> <pre><code>&lt;%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" %&gt; &lt;script type="text/C#" runat="server"&gt; // Server side script in the code behind that will receive // the value: The method needs to be static // and decorated with the WebMethod attribute [System.Web.Services.WebMethod] public static string Foo(string id) { return "ok"; } &lt;/script&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;link href="~/Styles/Site.css" rel="stylesheet" type="text/css" /&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function () { // Send an AJAX request to the server $.ajax({ url: '/default.aspx/foo', type: 'POST', contentType: 'application/json; charset=utf-8', // Pass the value as JSON string // You might need to include json2.js for the JSON.stringify // method: http://www.json.org/json2.js data: JSON.stringify({ id: 'someId123' }), success: function (result) { // The result is also JSON alert(result.d); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="Form1" runat="server"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload