AJAX reference PageMethod method description in ASP. NET 3.5

Source: Internet
Author: User
Tags microsoft website

Sometimes, we need to use the post-commit method on the page's response surface. The following method can be implemented.

1. Using AJAX to support the response.

Add the following labels to the system. web tag of web. config:

 

Code

<HttpHandlers>
<Add verb = "GET, HEAD" path = "ScriptResource. axd "type =" System. web. handlers. scriptResourceHandler, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "validate =" false "/>
</HttpHandlers>
<HttpModules>
<Add name = "ScriptModule" type = "System. Web. Handlers. ScriptModule, System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
</HttpModules>

 

 

2. Use the EnablePageMethod annotation of ScriptManage.

 

ScriptManager. GetCurrent (Page). EnablePageMethods = true;

 

 

3. Add the method to be used in future generation.

Ps: Add webmethod and static.

Code

[System. Web. Services. WebMethod]
Public static DataTable GetData (string empno, string cname)
{
Try
{
DataTable dt = new DataTable ();
Dt. Columns. Add (new DataColumn (empno, typeof (string )));
Dt. Columns. Add (new DataColumn (cname, typeof (string )));
Return dt;
}
Catch
{
Return null;
}
}

 

 

4. Use this method in javascript on the page.

 

Code

Function GetData (){
PageMethods. GetData (
Document. getElementById ("<% = TextBox_EmpNo.ClientID %>"). value,
Document. getElementById ("<% = TextBox_CName.ClientID %>"). value,
OnSuccess );
}
Function onSuccess (result ){
If (result! = Null ){
Var dt = result;
If (dt! = Null & typeof (dt) = "object "){
Var innerHtml = new Sys. StringBuilder ("<OL> ");
For (var I = 0; I <dt. rows. length; I ++ ){
Var empid = dt. rows [I]. employee_id;
Var chname = dt. rows [I]. chname;
InnerHtml. append (String. format ("<LI> <INPUT type = 'checkbox' id = 'all _ {0}, {1} <BR> {2} '> {3 }, {4} <br >{5} </LI> ", chname, empid, email, chname, empid, email ));
}
InnerHtml. append ("</OL> ");
Document. getElementById ("PH"). innerHTML = innerHtml;
}
}
Else {
Alert ("Error ");
Return false;
}
}
Function onFailed (error ){
Alert (error. get_message ());
}

 

 

PS: note that the PageMethods class is generated by the system's automatic token, which records all webmethod methods in the class.

 

Code

The method for using PageMethods volumes is as follows:
PageMethods. [MethodName] (parameter1, parameter2 ,..., SuccessMethod, FailedMethod, userContext );
Where
Parameter? To match the Server.
SuccessMethod, required. Number of response letters when WebMethod is correctly returned. The prototype is onSuccess (result ).
FailedMethod, not required. WebMethod returns the response number when the response is returned, which is an onFailed (error ).
 

 

 

After the preceding steps are completed, if the method used by the consumer returns a data type other than DataSet, able, and DataRow, then the result will be displayed.

However, if DataSet, able, or DataRow is returned, a response is returned.

 

A circular reference was detected while serializing an object...

 

Microsoft also knows this question.

Http://connect.microsoft.com/VisualStudio/feedback/details/535133/error-a-circular-reference-was-detected-while-serializing-an-object-of-type-system-data-dataview

However, no specific solution was provided.

The solutions found on the Internet are as follows:

1. download AJAX Futures CTP from the microsoft website

Http://www.microsoft.com/downloads/details.aspx? FamilyID = 4cb52ea3-9548-4064-8137-09b96af97617 & displaylang = en

2. copy the Microsoft. Web. Preview. dll file to the bin directory of the project.

3. modify web. config and add the following labels.

 

Code

<System. web. extensions>
<Scripting>
<WebServices>
<JsonSerialization maxJsonLength = "500000000">
<Converters>
<Add name = "DataSetConverter" type = "Microsoft. web. preview. script. serialization. converters. dataSetConverter, Microsoft. web. preview, Version = 1.0.61025.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "/>
<Add name = "DataRowConverter" type = "Microsoft. web. preview. script. serialization. converters. dataRowConverter, Microsoft. web. preview, Version = 1.0.61025.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "/>
<Add name = "DataTableConverter" type = "Microsoft. web. preview. script. serialization. converters. dataTableConverter, Microsoft. web. preview, Version = 1.0.61025.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "/>
</Converters>
</JsonSerialization>
</WebServices>
</Scripting>
</System. web. extensions>

 

 

This solves the problem.

 

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.