Serialize a web form and pass to a [WebMethod]

You can use the webfu.callPage() and webfu.serializePage() functions in WFUtilitiesJquery.js...
http://webformsutilities.codeplex.com/SourceControl/changeset/view/12623#242221

For example:

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

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

Now, in the WebMethod itself, you will need to use the WFUtilities.cs UrlDecodeDictionary method...
http://webformsutilities.codeplex.com/SourceControl/changeset/view/9100#203151
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

Comments

No comments yet.