asp.net ajax JSON starter tutorials with detailed examples

Source: Internet
Author: User

ASP tutorial. NET AJAX JSON Introduction tutorial and detailed instance JSON

Before we discuss the JSON format formally, let's briefly recall XML. XML is the abbreviation for "Extensible Markup Language," which provides a way to define a series of data transfer protocols in the Web, and is text-type, and is hailed as "an ideal way to fully develop the Internet and web potential".

So why do you want to introduce JSON in the ASP.net tutorial ajax? Let's take a look at the examples first. For example, the current Web page will load some address book information from the background, if written in XML, it may be the following form:

<contact>
<friend>
<name>michael</name>
<email>17bity@gmail.com</email>
</friend>
<friend>
<name>john</name>
<email>john@gmail.com</email>
</friend>
<friend>
<name>peggy</name>
<email>peggy@gmail.com</email>
</friend>
</contact>

  

And in JSON form, it would be:

[
Friend: {
Name: "Michael",
Email: "17bity@gmail.com",
Homepage: "Http://www.111cn.net"
},
Friend: {
Name: "John",
Email: "John@gmail.com",
Homepage: "Http://www.111cn.net"
},
Friend: {
Name: "Peggy",
Email: "Peggy@gmail.com",
Homepage: "Http://mb.111cn.net"
}
]


Use class

using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
User's summary description
</summary>
public class users
{
String name;
public string Name
{
get {return name;}
set {name = value;}
}
String age;
public string Age
{
get {return age;}
set {age = value;}
}
}

JS Code

<script type= "Text/jscript" >
function CallServer () {
JSON Send Object
Serversum ("{name: ' Linyijia ', Age: ' 21 '}");
}
function Getregister (RG, Contex) {
document.getElementById ("Txtregister"). Value=rg;
}
</script>

asp.net

using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.web.script.serialization;
Public partial class _default:system.web.ui.page, ICallbackEventHandler
{
Users u = NULL;
protected void Page_Load (object sender, EventArgs e)
{
Callback Getregister method
String callbackfun = Page.clientscript.getcallbackeventreference (This, "Arg", "Getregister", "context");
Creates the Serversum method, which, when invoked by the client, recalls the Getregister method, passes the argument to the RaiseCallbackEvent (string eventargument), and finally passes the
The GetCallbackResult () method passes the return value to the client
String registerfun = String.Format ("function serversum (arg,context) {{0};}}", Callbackfun);
Page.ClientScript.RegisterClientScriptBlock (This.gettype (), "Serversum", registerfun,true);
}
string mssage = String.Empty;
#region ICallbackEventHandler Members
public string GetCallbackResult ()
{
Return "server: Hello, your username is:" + u.name + "Your age is" + u.age;
}
public void RaiseCallbackEvent (String eventargument)
{
Web Effects Serializer js = new JavaScriptSerializer ();
U =js.deserialize<users> (eventargument);
}
#endregion
}

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.