This example is based on Ajax. the data type example by the author of. NET is probably used to pass a lot of data types as parameters to the server-side Ajax.. Net method, also from Ajax.. NET Server method returns the same data type. In particular, he can also convert a client object into an XML document. The author calls him ijavascriptobject,CodeFor more information, see the following.
Front-end datatype. aspx code:
<% @ Page Language = "C #" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<SCRIPT runat = "server">
Void page_load (Object sender, eventargs E)
{
// Register the class mydatatype
Ajaxpro. Utility. registertypeforajax (typeof (mydemo. mydatatype ));
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input type = button value = "test bool type" onclick = "getboll ();"/> <br/>
<Input type = button value = "test int type" onclick = "getint ();"/> <br/>
<Input type = button value = "test double type" onclick = "getdouble ();"/> <br/>
<Input type = button value = "test char type" onclick = "getchar ();"/> <br/>
<Input type = button value = "Pass int, double, float to the server" onclick = "getnumber ();"/> <br/>
<Input type = button value = "Incoming int array, double array, return object array" onclick = "getnumbers ();"/> <br/>
<Input type = button value = "Incoming datetime type" onclick = "getdatetime ();"/> <br/>
<Input type = button value = "Incoming object, XML" onclick = "getxml ();"/> <br/>
</Div>
</Form>
<SCRIPT type = "text/JavaScript">
Function getboll ()
{
// Call the server method to return the bool type
Alert (mydemo. mydatatype. getbool (false). value );
}
Function getint ()
{
// Call the server method to add 10 to the passed int and return it back.
Alert (mydemo. mydatatype. getint (20). value );
}
Function getdouble ()
{
// Transmits a double value to the server and returns a double value.
Alert (mydemo. mydatatype. getdouble (0.01). value );
}
Function getchar ()
{
// Pass a char to the server and return the char after + 1
Alert (mydemo. mydatatype. getchar ('A'). value );
}
Function getnumber ()
{
// Pass an int, a double, and a float to the server, and return their sum with an int
Alert (mydemo. mydatatype. getnumber (3, 0.5, 0.8). value );
}
Function getnumbers ()
{
// Pass an int array and a double array to the server. The server calculates the sum of each array and returns the result as an object array.
VaR P = mydemo. mydatatype. getnumbers ([1.2, 9], [5.02, 55,]). value;
Alert ("int array and is:" + P [0] + "\ ndouble array and is:" + P [1]);
}
Function getdatetime ()
{
// Pass the current time to the server and return the current time plus 4 hours
// Note that the current time obtained by the client is new date () instead of datetime. Now.
Alert (mydemo. mydatatype. getdatetime (new date (). value );
}
Function getxml ()
{
// Initialize an object first. Note that the o in front of the object is capitalized.
VaR o = new object ();
O. myarray = [1, 2, 3, 4, 5, 6]; // o has an attribute. myarray is an array.
O. mybool = true; // o has an attribute mybool, whose type is bool.
O. mystring = "I love you! "; // O has a property mystring, type string
// O there is also an object type attribute, and myobject has two more attributes
O. myobject = {"firstname": "rat", "lastname": "Master Wu qiwa "};
// O has a datetime attribute.
O. mydate = new date ();
// Call the server method to send the object to the server. The server converts the object to XML and returns the result.
VaR A = mydemo. mydatatype. getxml (O). value;
Alert ();
}
</SCRIPT>
</Body>
</Html>
Class file mydatatype. CS code:
Using system;
Using system. Data;
Using system. configuration;
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 ajaxpro;
Using system. xml;
/// <Summary>
/// Summary description for mydatatype
/// </Summary>
///
Namespace mydemo
{
Public class mydatatype
{
Public mydatatype ()
{
//
// Todo: Add constructor logic here
//
}
[Ajaxpro. ajaxmethod]
Public bool getbool (bool B)
{
// If the parameter is set to true, false is returned. If the parameter is set to false, true is returned.
Return! B;
}
[Ajaxpro. ajaxmethod]
Public int getint (int I)
{
// Add 10 to the incoming int parameter.
Return I + 10;
}
[Ajaxpro. ajaxmethod]
Public double getdouble (double D)
{
// Return the sent double + 0.1
Return D + 0.1;
}
[Ajaxpro. ajaxmethod]
Public char getchar (char C)
{
// Add 1 to the sent char
Return ++ C;
}
[Ajaxpro. ajaxmethod]
Public int getnumber (int I, double B, float F)
{
// Calculate their sum and return with int
Return (INT) (double) I + B + F );
}
[Ajaxpro. ajaxmethod]
Public object [] getnumbers (INT [] I, double [] D)
{
// The input parameter is an int array and a double array. We calculate the sum of the two arrays,
// Then use them as an element of an object array.
Object [] O = new object [2]; // Initialize an object array with two elements
O [0] = 0; // assign a value to the first element. It will be used to hold the sum of the incoming int Array
O [1] = 0; // assign a value to the second element. It will be used to hold the sum of the double Array
// The Int array sent from the repeat. Put the sum in the first element of the object.
Int I1 = 0;
Foreach (int A in I)
{
I1 + =;
}
O [0] = I1;
// Returns the double array.
Double d1 = 0;
Foreach (double B in D)
{
D1 + = B;
}
O [1] = D1;
Return O;
}
[Ajaxpro. ajaxmethod]
Public datetime getdatetime (datetime D)
{
// Add the sent time to four hours to return
Return D. addhours (4 );
}
[Ajaxpro. ajaxmethod]
Public String getxml (ijavascriptobject O)
{
// The object on the client is converted into the ijavascriptobject type.
// Convert it to xml dom, and use the javascriptutil method to pay attention to using ajaxpro
Xmldocument xml = javascriptutil. converti.pdf criptobjecttoxml (O );
Return XML. outerxml;
}
}
}
The running result is as follows: