Use AJAX to bind data islands with no need for refreshing

Source: Internet
Author: User

First we will use Ajax. NET Professional today, you can get relevant information about AJAX. NET from http://www.schwarz-interactive.de. You also need to know about XML data island.

Next we will look at a simple binding example:

Step 1 determine the XML Data Source

<Xml ID = "xmlData" name = "xmlData">

<Root>

<METADATA>

<AUTHOR> John Smith </AUTHOR>

<GENERATOR> Visual Notepad </GENERATOR>

<PAGETYPE> Reference </PAGETYPE>

<ABSTRACT> Specifies a data island </ABSTRACT>

</METADATA>

<METADATA>

<AUTHOR> John Smith2 </AUTHOR>

<GENERATOR> Visual Notepad2 </GENERATOR>

<PAGETYPE> Reference2 </PAGETYPE>

<ABSTRACT> Specifies a data island2 </ABSTRACT>

</METADATA>

<METADATA>

<AUTHOR> John Smith3 </AUTHOR>

<GENERATOR> Visual Notepad3 </GENERATOR>

<PAGETYPE> referenpushed </PAGETYPE>

<ABSTRACT> Specifies a data island3 </ABSTRACT>

</METADATA>

</Root>

</Xml>

Step 2: bind the container. Here we use Table

<TABLE dataSrc = "# xmlData" border = 1>

<TR>

<TD> <span dataworks = "AUTHOR"> loading... </span> </TD>

<TD> <span dataworks = "GENERATOR"> loading... </span> </TD>

<TD> <span dataworks = "PAGETYPE"> loading... </span> </TD>

<TD> <span dataworks = "ABSTRACT"> loading... </span> </TD>

</TR>

</TABLE>

Copy the two pieces of code to your HTMl page and run it to see the effect.

The first section is our XML data source, that is, the data to be bound. The METADATA node is equivalent to the table name, AUTHOR, GENERATOR, PAGETYPE, and ABSTRACT are equivalent to the column names, the following Table is a container used to display data. <TABLE dataSrc = "# xmlData" border = 1> is the data source of the specified Table. <span dataworks = "AUTHOR"> loading... </span> is used to bind the field name.

Now, let's take a look at how to use Ajax to implement the unrefresh binding.

Step 1: Configure Ajax and add the configuration section to the Web. config file:

<HttpHandlers>

<Add verb = "POST, GET" path = "ajaxpro/*. ashx" type = "AjaxPro. AjaxHandlerFactory, AjaxPro"/>

</HttpHandlers>

Step 2: write the code for the returned Dataset:

Public class MyClass

{

[AjaxMethod]

Public string GetDataSet ()

{

DataSet dst = new DataSet ();

DataTable dt = new DataTable ();

Dt. Columns. Add ("Text ");

Dt. Columns. Add ("Number ");

Random random = new Random (Guid. NewGuid (). GetHashCode ());

For (int I = 0; I <random. Next (10, 20); I ++)

{

DataRow row = dt. NewRow ();

Row ["Text"] = Guid. NewGuid (). ToString ("N ");

Row ["Number"] = random. Next (100 );

Dt. Rows. Add (row );

}

Dst. Tables. Add (dt );

String text = "";

Using (MemoryStream MS = new MemoryStream ())

{

Dst. WriteXml (MS );

Ms. Position = 0;

StreamReader sr = new StreamReader (MS );

Text = sr. ReadToEnd ();

}

Return text;

}

}

Step 3: register this method in Page_Load. AjaxPro. Utility. RegisterTypeForAjax (typeof (WebTest1.AjaxTest1); (WebTest1 is the namespace name)

Step 4: Add client binding

<INPUT onclick = "WebTest1.MyClass. GetDataSet (callback)" type = "button" value = "GetDataSet">

<Div id = "oDataPanel">

</Div>

<TABLE datasrc = "# xmlData" WIDTH = "500" BORDER = "1"

CELLSPACING = "1" CELLPADDING = "1">

<Thead>

<Tr>

<Th width = "70%" type = "parmname" text = "parmname"> name </th type = "parmname" text = "/parmname">

& Lt; th width = "30%" & gt; age & lt;/th & gt;

</Tr>

</Thead>

<TR>

<TD> <span dataworks = "Text"> </span> </TD>

<TD> <span dataworks = "Number"> </span> </TD>

</TR>

</TABLE>

<Div id = "oDataPanel"> is the container used to store Xml data sources.

Step 5: Compile the JavaScript code for loading data

Function callback (res)

{

If (! Res. error)

{

Document. all. oDataPanel. innerHTML = <xml id = "xmlData"> + res. value + </xml>;

}

Else

{

Alert (res. error. Message );

}

}

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.