c # source data transfer format Json

Learn something. Leave footprints.

To achieve the effect, click the button, the background load data source, the front desk is bound to drop-down box.

Reception:

 
   
<% @ Page Language = " C# " AutoEventWireup = " true " CodeBehind = " JsonTrans.aspx.cs " Inherits = " auotoCompleteText.JsonTrans " %>

<! 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 ></ title >
< script type ="text/javascript" src ="jquery-1.3.2-vsdoc2.js" ></ script >
< script type ="text/javascript" >
function loadValue() {
$.get(
" JsonTransHander.ashx " , function (jsonData) {
debugger ;
var json = eval(jsonData);
for ( var i = 0 ; i < json.length; i ++ ) {
// js
// var op = new Option(json[i].Name, json[i].ID);
// document.getElementById("Select1").options.add(op);

// jquery
$( " #Select1 " ).append( " <option value=' " + json[i].value + " '> " + json[i].Name + " </option> " );
}
});
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< select id ="Select1" >
</ select >
< input type ="button" value ="Load" onclick ="loadValue();" />
</ div >
</ form >
</ body >
</ html >

 

Backstage:

Create a general handler named JsonTransHander.ashx. It reads as follows:

 
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace auotoCompleteText
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class JsonTransHander : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType
= " text/plain " ;
// 初始化数据
List < People > peopleList = new List < People > ();
for ( int i = 0 ; i < 10 ; i ++ )
{
People people
= new People() { ID = i,Name = " name " + i};
peopleList.Add(people);
}


// 引用System.Web.Extensions .net3.5框架
JavaScriptSerializer serializer = new JavaScriptSerializer();
// 转换
var jsonData = serializer.Serialize(peopleList);

// 返回
context.Response.Write(jsonData);
}
public bool IsReusable
{
get
{
return false ;
}
}
}

public class People
{
public string Name
{
set ;
get ;
}
public int ID
{
set ;
get ;
}
}
}

 

Effect: an empty drop-down box, click on the button next to the drop-down box bound background data.

Reproduced in: https: //www.cnblogs.com/xinjian/archive/2010/11/23/1885225.html

Guess you like

Origin blog.csdn.net/weixin_34259159/article/details/93822262