System.Web.Script.Serialization命名空间

        .Net Framework 3.0的新特性,该命名空间提供了针对CLR托管类型的JSON(JavaScript Object Notation)的序列化和反序列化的方法。通过该命名空间提供的方法可以方便的实现服务器端托管类型数据与客户端浏览器之间JSON形式的数据传递。
        
        以下是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 >

        序列化后的字符串在客户端显示效果如图:


参考文章: ICallback & JSON Based JavaScript Serialization:ASP alliance

转载于:https://www.cnblogs.com/drummery/archive/2008/03/31/1130690.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值