<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default18.aspx.cs" Inherits="Default18" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Ajax JavaScriptSerializer(JSON)を使用する</title>
    <script type="text/javascript">
       
        function jsonObject() {
       
            this.value = '0';
            this.text = '';
       
        }
       
        function select1Selected() {
           
            var select1 = $get('Select1');
            var selectedIndex = $get('Select1').selectedIndex;
           
            var json = new jsonObject();
           
            json.value = selectedIndex;
            json.text = select1.options[selectedIndex].text;
           
            var jsonString = Sys.Serialization.JavaScriptSerializer.serialize(json);
           
            $get('Label1').innerHTML = jsonString;
            $get('HiddenField1').innerText = jsonString;
        }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
   
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
       
        <select id="Select1" onchange="select1Selected()">
            <option value='0'></option>
            <option value='1'>ASANO</option>
            <option value='2'>MOMOTCHI</option>
            <option value='3'>ASP.NET AJAX</option>
        </select>
       
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
       
        <br />
       
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
       
        <asp:HiddenField ID="HiddenField1" runat="server" />
       
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.Web.Script.Serialization;

public partial class Default18 : System.Web.UI.Page
{
    public class JsonObject
    {
        public string value = "0";
        public string text = String.Empty;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        string jsonText = HiddenField1.Value;

        JsonObject json = serializer.Deserialize<JsonObject>(jsonText);

        Label1.Text = String.Format("Value:{0} Text:{1}", json.value, json.text);
    }
}