Implement Ajax in ASP. NET (3)

Source: Internet
Author: User
Processing type

 

Return complex types

The Ajax Wrapper can not only process integers returned by the serversideadd function. It also supports integers, strings, double, booleans, datetime, datasets, datatables, custom classes, arrays, and other basic types. All other types return their tostring values.

The returned datasets is similar to the real. Net dataset. Assume that a server-side function returns dataset.CodeDisplay the content on the client:

<Script language = "JavaScript">

// Asynchronous call to the mythical "getdataset" server-side function

Function getdataset (){

Ajaxfunctions. getdataset (getdataset_callback );

}

Function getdataset_callback (response ){

VaR DS = response. value;

If (Ds! = NULL & typeof (DS) = "object" & Ds. tables! = NULL ){

VaR S = new array ();

S [S. Length] = "<Table border = 1> ";

For (VAR I = 0; I <Ds. Tables [0]. Rows. length; I ++ ){

S [S. Length] = "<tr> ";

S [S. Length] = "<TD>" + Ds. Tables [0]. Rows [I]. firstname + "</TD> ";

S [S. Length] = "<TD>" + Ds. Tables [0]. Rows [I]. Birthday + "</TD> ";

S [S. Length] = "</tr> ";

}

S [S. Length] = "</table> ";

Tabledisplay. innerhtml = S. Join ("");

}

Else {

Alert ("error. [3001]" + response. Request. responsetext );

}

}

</SCRIPT>

AJAX can also return custom classes. The only requirement is that the serializable attribute must be used. Suppose there are the following classes:

[Serializable ()]

Public class user {

Private int _ userid;

Private string _ firstname;

Private string _ lastname;

Public int userid {

Get {return _ userid ;}

}

Public String firstname {

Get {return _ firstname ;}

}

Public String lastname {

Get {return _ lastname ;}

}

Public user (INT _ userid, string _ firstname, string _ lastname ){

This. _ userid = _ userid;

This. _ firstname = _ firstname;

This. _ lastname = _ lastname;

}

Public user (){}

[Ajaxmethod ()]

Public static user getuser (INT userid ){

// Replace this with a DB hit or something

Return new user (userid, "Michael", "Schwarz ");

}

}

You can register the getuser agent by calling registertypeforajax:

Private void page_load (Object sender, eventargs e ){

Utility. registertypeforajax (typeof (User ));

}

In this way, you can call getuser asynchronously on the client:

<Script language = "JavaScript">

Function getuser (userid ){

User. getuser (getuser_callback );

}

Function getuser_callback (response ){

If (response! = NULL & response. value! = NULL ){

VaR user = response. value;

If (typeof (User) = "object "){

Alert (user. firstname + "" + User. lastname );

}

}

}

Getuser (1 );

</SCRIPT>

The value returned in the response is actually an object that exposes the same attributes (firstname, lastname, and userid) as the server object ).

Http://www.shpan.com/Detail.asp? Id = 398

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.