Serialize a web form and pass to a [WebMethod]

You can use the webfu.callPage() and webfu.serializePage() functions in WFUtilitiesJquery.js...

For example:

Create a public/static (VB public/shared) [WebMethod] that accepts a single string "formValues".
public static AjaxPostback(string formValues) { }

Call this method via AJAX, ie:
webfu.callPage("Default.aspx/AjaxPostback", {
  data: "{\"formValues\":\"" + webfu.serializePage() + "\"}",
  success: function(msg) {
  error: function(a) { alert(a.responseText); }

Now, in the WebMethod itself, you will need to use the WFUtilities.cs UrlDecodeDictionary method...
Dictionary<string, string> request = WFUtilities.UrlDecodeDictionary(formValues);
//Can now access like this
string firstName = request["txtFirstname"];
//Or use methods like TryValidateModel and UpdateModel which have overloads that accept a Dictionary<string,string>

Last edited May 19, 2012 at 1:20 AM by aikeru, version 1


No comments yet.