System. Web. Script. serialization namespace

Source: Internet
Author: User
New Features of. NET Framework 3.0. This namespace provides serialization and deserialization methods for CLR-hosted JSON (JavaScript Object Notation. The namespace provides a way to easily transfer data in JSON format between the hosted data on the server and the browser on the client.

The following is the sample code snippet provided by msdn: 1 <% @ page Language = "C #" %>
2 <% @ import namespace = "system. Web. Script. serialization" %>
3
4
5
6 <SCRIPT runat = "server">
7
8. javascriptserializer serializer;
9 protected void page_load (Object sender, eventargs E)
10 {
11 serializer = new javascriptserializer ();
12
13 // register the custom converter.
14 serializer. registerconverters (New javascriptconverter [] {
15 New system. Web. Script. serialization. cs. listitemcollectionconverter ()});
16 this. setfocus (textbox1 );
17}
18
19 protected void savebutton_click (Object sender, eventargs E)
20 {
21 // Save the current state of the ListBox control.
22 savedstate. Text = serializer. serialize (listbox1.items );
23 recoverbutton. Enabled = true;
24 message. Text = "State saved ";
25}
26
27 protected void recoverbutton_click (Object sender, eventargs E)
28 {
29 // recover the saved items of The ListBox control.
30 listitemcollection recoveredlist = serializer. deserialize <listitemcollection> (savedstate. Text );
31 listitem [] newlistitemarray = new listitem [recoveredlist. Count];
32 recoveredlist. copyto (newlistitemarray, 0 );
33 listbox1.items. Clear ();
34 listbox1.items. addrange (newlistitemarray );
35 message. Text = "last saved state recovered .";
36}
37
38 protected void clearbutton_click (Object sender, eventargs E)
39 {
40 // remove all items from The ListBox control.
41 listbox1.items. Clear ();
42 message. Text = "all items removed ";
43}
44
45 protected void contactsgrid_selectedindexchanged (Object sender, eventargs E)
46 {
47 // get the currently selected row using the selectedrow property.
48 gridviewrow ROW = contactsgrid. selectedrow;
49
50 // get the ID of item selected.
51 string Itemid = contactsgrid. datakeys [row. rowindex]. value. tostring ();
52 listitem newitem = new listitem (row. cells [4]. Text, Itemid );
53
54 // check if the item already exists in the ListBox control.
55 if (! Listbox1.items. Contains (newitem ))
56 {
57 // Add the item to the ListBox control.
58 listbox1.items. Add (newitem );
59 message. Text = "item added ";
60}
61 else
62 message. Text = "item already exists ";
63}
64
65 protected void contactsgrid_pageindexchanged (Object sender, eventargs E)
66 {
67 // reset the selected index.
68 contactsgrid. selectedindex =-1;
69}
70
71 protected void searchbutton_click (Object sender, eventargs E)
72 {
73 // reset indexes.
74 contactsgrid. selectedindex =-1;
75 contactsgrid. pageindex = 0;
76
77 // set focus on the Textbox Control.
78 scriptmanager1.setfocus (textbox1 );
79}
80
81 protected void checkbox1_checkedchanged (Object sender, eventargs E)
82 {
83 // show/hide the Saved state string.
84 savedstate. Visible = checkbox1.checked;
85 statelabel. Visible = checkbox1.checked;
86}
87 </SCRIPT>

The serialized string is displayed on the client.

Reference: icallback & JSON based JavaScript serialization: ASP Alliance

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.