Ajax call background datatable

Source: Internet
Author: User

Previously encountered problems were not implemented using the ajaxpro method. Later, the jquery. Ajax method was used to call the datatable returned by the webmethod in the background. If you have any questions about converting a able into a JSON format, I will write an example to describe the implementation process.

1. Create testajax2.aspx;

<Body>
<Form ID = "form1" runat = "server">
<Input id = "btnjson" type = "button" value = "JSON"/>
<Div class = "case_input1" id = "article">
<Ul>
</Ul>

</Div>
</Form>
</Body>

 

2. Add reference system. Web. extension.

Add namespace

Using system. Web. Services;
Using system. collections;
Using system. Web. Script. serialization;

3. construct the webmethod method in the. CS file and return the datatable type.

[Webmethod]
Public static string getdatatable (string CID, string site)
{
Dataset DS = new usercase (). getresult (CID, site, String. Empty );
String S = dttojson (Ds. Tables [0]);
Return S. tostring ();
}

 

4. Add a method to convert a able to JSON.

Public static string dttojson (datatable DT)
{
Javascriptserializer JSS = new javascriptserializer ();
Arraylist DIC = new arraylist ();
Foreach (datarow row in DT. Rows)
{
Dictionary <string, Object> Drow = new dictionary <string, Object> ();
Foreach (datacolumn Col in DT. columns)
{
Drow. Add (Col. columnname, row [col. columnname]);
}
Dic. Add (Drow );
}
Return JSS. serialize (DIC );
}

5. Use jquery to call Ajax on the aspx page.

<SCRIPT type = "text/JavaScript" src = "jquery-1.6.2.js"> </SCRIPT>
<Script language = "JavaScript" type = "text/JavaScript">
$ (Function (){
$ ("# Btnjson"). Click (function (){
$. Ajax ({
Type: "Post ",
URL :" Testajax2.aspx/getrelatedarticle ", // URL page/method name
Data: "{'cid': 'c0503', 'SITE': 'tp '}", // parameter {key/value}
Contenttype: "application/JSON; charset = UTF-8 ",
Datatype: "JSON ",
Success: function (data ){
Data = jquery. Parsejson (Data. D );
$. Each (data, function (I, item ){
$ ("# Article ul "). append ('<li> <a href = "' + item. href + '"target = _ blank>' + item. title + '</a> </LI> ')
})

},
Error: function (error ){
Alert (error );
}
});
})
})
</SCRIPT>

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.