The Jquery combination form element is in JSON format, asp.net deserialized _jquery

Source: Internet
Author: User
Author: Oswei email:ikmb@163.com reprint Annotated author
Note: 1, JS according to the Table element class attribute, the form element name and value are combined into JSON format, the table cell element class attribute can be targeted to combine JSON data.
2. The back-end asp.net is deserialized into the object's real column with JavaScriptSerializer.
3, Benefits: simplifies the front-end data reading and the back-end data assignment.
Copy Code code as follows:

function Getjsonstr (class_name) {
var a = [];
text box
$ ("." + class_name). Filter (": Text"). each (function (i) {
alert (this.name);
alert (this.value);
A.push ({name:this.name, value:this.value});

});
Drop down list
$ ("." + class_name). Filter ("select"). each (function (i) {
alert (this.name);
alert (this.value);
A.push ({name:this.name, value:this.value});

});
Single selection box
$ ("." + class_name). Filter (": Radio"). Filter (": Checked"). each (function (i) {
alert (this.name);
alert (this.value);
A.push ({name:this.name, value:this.value});
});
check box start
var TEMP_CB = "";
$ ("." + class_name). Filter (": CheckBox"). Filter (": Checked"). each (function (i) {
if (Temp_cb.indexof (this.name) = =-1) {
TEMP_CB + + THIS.name + ",";
}

});
var Temp_cb_arr = Temp_cb.split (",");
var cb_name = "";
var cb_value = "";
for (var temp_cb_i = 0; temp_cb_i < temp_cb_arr.length-1; temp_cb_i++) {
Cb_name = Temp_cb_arr[temp_cb_i];
var cb_value_length = $ ("input[name= '" + temp_cb_arr[temp_cb_i] + "']:checked"). Length;
$ ("input[name= '" + temp_cb_arr[temp_cb_i] + "']:checked"). each (function (i) {
if (i = = cb_value_length-1)
Cb_value + = This.value;
Else
Cb_value + + This.value + ",";

});
alert (cb_name);
alert (Cb_value);
A.push ({name:cb_name, value:cb_value});
}
check box end


Group to JSON
var Temp_json = "";
for (var json_i = 0; json_i < a.length; json_i++) {
if (json_i!= a.length-1) {
Temp_json + = ' "' + A[json_i].name + '": "' + A[json_i].value + '", ';
}
else {
Temp_json + = ' "' + A[json_i].name + '": "' + A[json_i].value + '";
}
}
Return "{" + Temp_json + "}";
}

asp.net
Copy Code code as follows:

Public partial class Test:System.Web.UI.Page
{
protected void Pag E_load (object sender, EventArgs e)
{
JavaScriptSerializer serializer = new JavaScriptSerializer ();
String r = request.form["MSG"];

//{"Name": "MyName1", "single": "One"}

T_json t_json_object = serializer.deserialize<t_json> (r) ;

Response.Write (t_json_object. Name);
Response.End ();
}
}

class T_json
{
public DateTime Name;
public string Single;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.