Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can post the form data to web method in aspx, <strong>by serializing the form data into an array, Stringify it into a json</strong>.</p> <p>In the server side, <strong>you will have to create a custom type containing a name value pair and the parameter of the page method will be an array of that custom type</strong>. The reason being, the json serialized array should be able to be deserialized into the parameter of the web method for the ajax call to happen.</p> <pre><code> function btnclick() { var array = $("#form1").serializeArray(); $.ajax({ type: "POST", url: "PageMethodTest.aspx/TestFormPost", data: JSON.stringify({ 'namevaluepair': $("#form1").serializeArray() }), contentType: "application/json; charset=utf-8", dataType: "json", success: fnsuccesscallback, error: fnerrorcallback }); } function fnsuccesscallback(data) { alert(data.d); } function fnerrorcallback(result) { alert(result.statusText); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager EnablePageMethods="true" runat="server"&gt; &lt;/asp:ScriptManager&gt; &lt;div&gt; &lt;table runat="server"&gt; &lt;tr&gt; &lt;td&gt; &lt;label id="lblName" runat="server" title="Name"&gt; Name&lt;/label&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" id="txtName" runat="server" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;label id="lblCompany" runat="server" title="Company"&gt; Company&lt;/label&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" id="txtCompany" runat="server" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type="button" style="width: 104px" value="Submit" onclick="btnclick();" /&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>Server code:</p> <pre><code> [WebMethod, ScriptMethod] public static String TestFormPost(NameValue[] namevaluepair) { StringBuilder builder = new StringBuilder(); foreach(NameValue nvp in namevaluepair) { builder.Append(nvp.Name + ": " + nvp.Value + ";"); } return builder.ToString(); } } public class NameValue { public String Name; public String Value; } </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