Ajax client calls the Server Page Method

Source: Internet
Author: User
Tags tojson

1. ClientCodeAs follows:

// Function: the client calls the webpage server method.

// Example:

// Parameter description:

// Isstaticmethod: whether it is a static method

// Methodname: Method Name

// Methodparamter: [Optional] method parameter, which must be an instance of Type methodparamter or a null value (No parameter)
// Callbackmethod: [Optional] The client Method for callback after the method is called. The client method is in the form of function callbackmethod (result) {}. result is a JSON object, for example: function handlechecklistresult (result) {}. The parameter value is 'handlechecklistresresult'
// Assemblyandclassname: [Optional] component and Class Name of the server on the page, in the form of 'assemblyname | classfullname', for example, weiky. dll | weiky. accessoriesform'
Function callpagemethod (isstaticmethod, methodname, methodparamter, callbackmethod, assemblyandclassname)
...{
If (methodparamter & typeof (methodparamter. addboolparamter )! = 'Function ')
...{
Alert ("The methodparamter parameter must be an instance of Type methodparamter or a null value ");
Return;
}
If (assemblyandclassname = NULL)
...{
If (typeof (assemblyandclassname )! = 'Undefined ')
...{
Assemblyandclassname = assemblyandclassname;
}
Else
...{
Alert ("components and class names of the Page Server are not provided ");
Return;
}
}
Try
...{
Mywebservice. callpagemethod (assemblyandclassname, isstaticmethod, methodname, methodparamter? Methodparamter. tojson (): NULL, methodparamter. totype (), callbackmethod? Callbackmethod: '', callbackbywebservice, handleservicemethodcallerror );
}
Catch (ERR)
...{
Alert ('failed to convert the parameter to a JSON object! ');
}
}

Function callbackbywebservice (result)
...{
VaR JSON = convertstringtojson (result );
If (JSON. type! = 0)
...{
Showmessagebox2 (JSON );
}
Else
...{
VaR callbackmethod = JSON. highlevelmessage;
If (callbackmethod! = '')
...{
JSON. highlevelmessage = '';
JSON. Message = replacestring (JSON. Message, 'response ','');
Eval (callbackmethod + '(JSON )');
}
}
}

Function methodparamter ()
...{
VaR paramter = '';
VaR JSON = NULL;

This. addstringparamter = function (value)
...{
Addparamter ('string', replacestring (value, '"', '\"'), '', 'stopped '));
}

This. addguidparamter = function (value)
...{
Addparamter ('guid ', value );
}

This. adddateparamter = function (value)
...{
Addparamter ('date', value );
}

This. addintparamter = function (value)
...{
Addparamter ('int', value );
}

This. adddecimalparamter = function (value)
...{
Addparamter ('decimal', value );
}

This. addboolparamter = function (value)
...{
Addparamter ('bool ', value );
}

Function addparamter (type, value)
...{
If (paramter! = '')
...{
Paramter + = ','
}
Paramter + = '{"type": "' + Type + '", "value": "' + value + '"}';
}

This. addjsonparamter = function (P)
...{
JSON = P;
}

This. tojson = function ()
...{
If (JSON)
...{
Return JSON;
}
If (paramter! = '')
...{
Return eval ('[' + paramter + ']');
}

Return NULL;
}

This. totype = function ()
...{
Return JSON? 1:0;
}
}
2. the WebService on the server is provided to the scriptmanager control. The WebService code is as follows:

[System. Web. Script. Services. scriptservice]
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Toolboxitem (false)]
Public class mywebservice: system. Web. Services. WebService
...{
[Webmethod (enablesession = true)]
Public String callpagemethod (string assemblyandclassname, bool isstaticmethod, string methodname, object paramterspackage, int mptype, string callbackmethod)
...{
Try
...{
Object result = "";
Bool succeed = false;
If (isstaticmethod)
...{
Type type = getactualtype (assemblyandclassname );
If (type! = NULL)
...{
Succeed = true;
If (mptype = 1)
...{
Result = type. invokemember (methodname, system. reflection. bindingflags. public | system. reflection. bindingflags. invokemethod | system. reflection. bindingflags. static, null, null, new object []... {paramterspackage });
}
Else
...{
Result = type. invokemember (methodname, system. reflection. bindingflags. public | system. reflection. bindingflags. invokemethod | system. reflection. bindingflags. static, null, null, getmethodargs (paramterspackage ));
}
}
}
Else
...{
Object o = webbase. getactualinstance (assemblyandclassname, this. server. mappath ("~ /Bin /"));
If (o! = NULL)
...{
Succeed = true;
If (mptype = 1)
...{
Result = O. getType (). invokemember (methodname, system. reflection. bindingflags. public | system. reflection. bindingflags. invokemethod | system. reflection. bindingflags. instance, null, O, new object []... {paramterspackage });
}
Else
...{
Result = O. getType (). invokemember (methodname, system. reflection. bindingflags. public | system. reflection. bindingflags. invokemethod | system. reflection. bindingflags. instance, null, O, getmethodargs (paramterspackage ));
}
}
}

return succeed? 0: 1, succeed? (Result = NULL? "": Result. tostring (): string. format ("failed to get component information, check whether the component parameter {0} is correct", assemblyandclassname);
}< br> catch (exception ERR)
... {
return err. message;
}< BR >}< br> private object [] getmethodargs (Object paramterspackage)
... {
If (paramterspackage = NULL) return NULL;

Int I = 0;
Object [] ARGs = new object [(object []) paramterspackage). Length];
Foreach (system. Collections. Generic. dictionary <string, Object> P in (object []) paramterspackage)
...{
Switch (P ["type"]. tostring (). tolower ())
...{
Case "string ":
ARGs [I ++] = P ["value"]. tostring (). Replace ("delimiter ","");
Break;
Case "guid ":
ARGs [I ++] = new GUID (P ["value"]. tostring ());
Break;
Case "date ":
ARGs [I ++] = convert. todatetime (P ["value"]. tostring ());
Break;
Case "int ":
ARGs [I ++] = convert. toint32 (P ["value"]. tostring ());
Break;
Case "decimal ":
ARGs [I ++] = convert. todecimal (P ["value"]. tostring ());
Break;
Case "bool ":
ARGs [I ++] = convert. toboolean (P ["value"]. tostring ());
Break;
Default:
ARGs [I ++] = P ["value"];
Break;
}
}
Return ARGs;
}
Private webbaseform getactualinstanceform (string assemblyandclassname)
...{
Object o = webbase. getactualinstance (assemblyandclassname, this. server. mappath ("~ /Bin /"));
If (o! = NULL)
...{
If (O is webbaseform)
...{
Return (webbaseform) O;
}
}

Return NULL;
}

Private type getactualtype (string assemblyandclassname)
...{
If (assemblyandclassname! = "")
...{
String [] AC = assemblyandclassname. Replace ("! "," \ "). Split ('| ');
If (AC. Length = 2)
...{
AC [0] = webbase. addpath (AC [0], this. server. mappath ("~ /Bin /"));
Return System. reflection. Assembly. loadfrom (AC [0]). GetType (AC [1]);
}
}

Return NULL;
}
}
3. Client call example:

Function dataddl_change (DDL)
...{
VaR MP = new methodparamter ();
MP. addintparamter (dropdownlist_getvalue (DDL ));
MP. addintparamter (entityobjectid );
Callpagemethod (true, 'getentitydata', MP, 'loadree REE ');
}

Function loaddatatree (JSON)
...{
Alert (JSON. Message );
}
Conclusion: through such encapsulation, It is very convenient for the client to call static/instance methods on the server side without causing any page PostBack. The client technologies used above include Ajax and JSON.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/weiky626/archive/2007/07/14/1690378.aspx

Related Article

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.