Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For a basic .NET website, I like to use the page methods for a quick easy way to make client to server calls. Consider the following aspx page and its code behind. You can expose public static server methods to javascript by adding the [WebMethod] attribute. Then you can call the function from javascript using PageMethods.NameOfFunction(). Just make sure you have a scriptmanager on the aspx page with the EnablePageMethods property set to "true".</p> <p><strong>Default.aspx</strong></p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript"&gt; function init() { var str = "bytes"; var data = []; for (var i = 0; i &lt; str.length; ++i) { data.push(str.charCodeAt(i)); data.push(0); } PageMethods.DoSomething(data); } &lt;/script&gt; &lt;/head&gt; &lt;body onload="init();"&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="ScriptMan" runat="server" EnablePageMethods="true"&gt; &lt;/asp:ScriptManager&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Default.aspx.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml.Linq; using System.Web.Services; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static void DoSomething(byte[] data) { //Do something with data from javascript } } </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