Ajax enables Web pages to display new data without refreshing them

Source: Internet
Author: User
Tags net reference return string
ajax| Data | refresh | page | show

With a ajaxpro:
1. Add a reference to the reference AjaxPro.dll, I use this, support asp.net 1.1 and asp.net 2.0.

2. Establishment of HttpHandler in Web.config

3. Create a new class demo, which provides a way to query the database and output lists. Gets the HTML that the server control generates after it runs, and then returns the client.

Demo.cs

---------------------------------------------

Using System;
Using System.Data;
Using Ajaxpro;
Using System.Web.UI.WebControls;

Namespace Ajaxdemo
{
public class Demo
{
Public Demo ()
{
}

[Ajaxmethod]
Public DataSet getsqlservertable ()
{
DataSet ds = new DataSet ();
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection ("User Id=sa;data source=server; Initial CATALOG=ZHSJ ");
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter ("SELECT ID from Invoice", conn);

Try
{
Conn. Open ();

Try
{
Da. Fill (DS);
}
Finally
{
Conn. Close ();
Conn. Dispose ();
}
}
catch (Exception)
{
}

return DS;
}

[Ajaxmethod]
public string getajaxtable ()
{
DataSet ds = new DataSet ();
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection ("User Id=sa;data source=server; Initial CATALOG=ZHSJ ");
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter ("SELECT Top 8 * from specification corresponding table", conn);

Try
{
Conn. Open ();

Try
{
Da. Fill (DS);
}
Finally
{
Conn. Close ();
Conn. Dispose ();
}
}
catch (Exception)
{
}


DataGrid dg=new DataGrid ();
Dg. Datasource=ds;
Dg. DataBind ();

Instantiate a HtmlTextWriter class
System.Text.StringBuilder sb=new System.Text.StringBuilder ();
System.IO.StringWriter sw=new System.IO.StringWriter (SB);
System.Web.UI.HtmlTextWriter htw=new System.Web.UI.HtmlTextWriter (SW);

Set the style of a DataGrid

Htw. AddAttribute ("BorderWidth", "1px", true);
Htw. AddAttribute ("BackColor", "White", true);
Htw. AddAttribute ("cellpadding", "4", true);
Htw. AddAttribute ("cellspacing", "2", true);
Htw. AddAttribute ("ForeColor", "#330099", true);


Dg. RenderControl (HTW);
String S=SB. ToString ();

return s;
}

}
}


4. Build a default.aspx, register in the Pageload event

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
AjaxPro.Utility.RegisterTypeForAjax (typeof (Ajaxdemo.demo));
}

5. Default.aspx the page code, the main part is the script

<script language= "JavaScript" defer>
function Dotest () {
AJAXDemo.Demo.GetSqlServerTable (_callback);
}

function _callback (res) {
var cols = res.value.tables[0]. Columns.length;
var rows = res.value.tables[0]. Rows.length;
Alert (cols + "cols and" + Rows + "Rows");
}

function Loadgrid () {
AJAXDemo.Demo.GetAjaxTable (_CALLBACK2);
}

function _callback2 (res) {
document.getElementById ("Uctd"). Innerhtml=res.value;
}

</script>

Window.settimeout (XXXXX, 1000);
Refresh every 1 seconds

Using Automatic no Refresh technology:
function xmlHttp (URL) {
var xmlccj= new ActiveXObject ("Microsoft.XMLHTTP")
Xmlccj.open ("Post", Url,false);
Xmlccj.send ();
var xmlhttps=bytes2bstr (xmlccj.responsebody);
return XMLHTTPS;
}
How to: (Parameter URL: page to extract data)
Two pages required, 1, page showing the data, 2, page to extract the data
Using this function on the page that shows the data, you can implement the data you want to extract without refreshing the update.
But to deal with Chinese problems, I will not say here, you can check the relevant information.



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.