找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 5699|回复: 2

System.Web.Script.Serialization命名空间 JSON

[复制链接]
发表于 2009-8-10 08:56:25 | 显示全部楼层 |阅读模式 IP:江苏扬州
.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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 23:31 , Processed in 0.193327 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表