ajax| Static | refresh | no Refresh | page
I. INTRODUCTION
We know that the ASP.net application fact is running on the server, the user's request to be sent to the remote server, the server executes the local program after the reload page to send the client. So there is a constant refresh of the problem, the page constantly flashing. The user is tired, the operation efficiency also greatly reduces, the server load increases. In fact, a client's request retrieves a response to user input at some point only by submitting a new request to the WEB server. In this case, developers can use JavaScript to load all the responses on the client, providing a better user experience. Unfortunately, in many cases, it's better not to return or load all the responses to JavaScript, just return the desired results, and the execution will still run on the server. AJAX provides a new intermediate option to leverage server-based applications while maintaining timely responsiveness and flexibility.
Ajax relies on the server as an intermediary to distribute and process requests. To do this, the. NET encapsulation class relies on the client's request object, and the XMLHttpRequest object is supported by most browsers, so using this object is a good solution.
Therefore, in order to achieve not refreshing the page, our client page is made into a static page. Static pages call a method of the. NET class through Ajax.NET. This is the most concise and efficient solution.
Second, how to apply Ajax.NET
1. Introduction of Ajax.dll documents in the project.
Ajax.dll is a class library file developed by Microsoft for use in asp.net. This class library encapsulates the implementation details of the XMLHttpRequest request server and is the solution technology of AJAX Knowledge application on the ASP.net platform. In a. NET project, you add a reference to it, and then you can start using the Ajax.dll package for development.
2. Set up Httphandle in Web.config
In order for it to work, the first step must be to install the Httphandle package in Web.config, not to explain in detail how httphandle works, we just need to understand that they can be used to process asp.net requests. For example, all requests for *.aspx can be sent through the System.Web.UI.PageHandlerFactory class to the control handle, simply saying, we put anything toward ajax/*. ASHX requests are sent to the Ajax.pagehandlerfactory request processing handle.
3. Writing service-side functions
Now we write server-side functions that can be invoked asynchronously by the client. Although it is not yet possible to support all of the return types, we still adhere to the server-side add functionality. In the page class of the Codebehind file, add the following method:
[Ajax.ajaxmethod ()]
public int Serversideadd (int firstnumber, int secondnumber)
{
return firstnumber + secondnumber;
}
Note that this function has a ajax.ajaxmethod () custom attribute, and the property service tells the Ajax wrapper class to create a JavaScript proxy for this method so that it can be invoked by the client.
III. Application Examples (click to download source code)
Our application is mainly to the operation of the database, the database data through the table rendering, the page to complete the increase, delete, update, query and other functions. More important is that it is a generic and very ingenious example of how to implement it. Any page is not refreshed and the code is very sophisticated. The following are implemented through Ajax technology.
• Establish engineering Httpforajax and introduce Ajax.dll files to your project.
• Add in your web.config.
• Create your HTML page.
1. Add HTML page infoclass.htm to the project. This page completes the query, the common tool and the data display and so on function.
The page mainly put four Div, one is the tree "Divtree". One is Chahi called "divfindtable", one is the tool bar called
"Divtoolbar", one is the data area is called "Divdatagrid". The following figure:
2. Add some references between and as follows:
Description: Xml.js is a few client programs written in JavaScript.
Mystyle.css is a style-style file.
Common.ashx is a common method for invoking Ajax.
Httpforajax.ashx is a reference to the class we're going to write with Ajax below. Where Httpforajax is the namespace of the project.
Ttyu.ajaxdata the class that you develop for yourself, where Ttyu is a namespace and Ajaxdata is the class name.
3. Scripting methods are written in JavaScript,
function Init () { Domtree=new Dhtmlxtreeobject (document.getElementById (' Divtree '), "100%", "100%", 0); Domtree.setimagepath ("imgs/"); Domtree.enabledraganddrop (True) Domtree.setdraghandler (Mydraghandler); Domtree.setonclickhandler (Selecttreenode); var s= ajaxdata.getxmltree (). value;
Domtree.loadxmlstring (s); Domtree.openitem ("r_1"); Selecttreenode ("r_1"); Divtoolbar.innerhtml=ajaxdata.gettoolbartable (tablename). Value; Divdatagrid.innerhtml=gettableouterhtml (tablename); } |
The Init method obtains the contents of the tree through the Getxmltree method of Ajax, Gettoolbartable gets the contents of the toolbar. The Gettableouterhtml method is used to obtain all the data of the physical table Tinfoclass and show it through the table.
function Find () { var table=ajaxdata.getxmlfindtable ("Grade name like '%" +txtname.value + "%"); Divdatagrid.innerhtml=table.value; } |
Query method Find () is an Ajax Getxmlfindtable method by grade name query and show the results.
function Openaddweb () { var retid =openaddwin (' infoclass_edit.aspx? Isadd=true ', 370,300); if (retid==-1) return; InsertRow (Datagrid1,retid); } |
The Openaddweb method opens the Infoclass_edit.aspx page to complete the add-data functionality on the page. If the return value is not 1, the new data is added and the data is inserted into the last row of the table. Retid represents the ID of the last line.
function Openeditweb (ThisCell) { var Id=thiscell.previoussibling.previoussibling.innertext; var retid =openaddwin ("infoclass_edit.aspx?id=" +id+ "&isadd=false", 370,300); if (retid==-1) return; There are update lines var currrow=thiscell.parentelement Setrowtext (Currrow); Setrowtext (Currrow,retdatarow); } |
The Openeditweb method is the ability to edit the row of data on the page after the user clicks the cell and opens the Infoclass_edit.aspx page. Replace the contents of the current row if there is an update line ...
• Create Ajax classes. (see file AjaxData.cs) The namespace is Ttyu and the class name is Ajaxdata.
1. Define the static variable MDT. MDT is a data table object that reads data from a physical table and holds it in memory. Our main operation is done by it. Defined as static is that we do not want to repeatedly access the database. We read it at once and use it permanently.
static public DataTable mDt; |
2. Define our main methods. Add [Ajax.ajaxmethod ()] to the uplink of each method.
Gets the data from the table tablename and returns it as an XML string. [Ajax.ajaxmethod ()] public string gettableouterhtml (string tablename) { DataTable dt=db. Db. Getdatatable (tablename); MDT=DT; Return DB. gettableouterhtml (dt. DefaultView); } |
The Gettableouterhtml method is read into memory by a physical table name and a table in XML format is obtained from its view. Here we read it and save it in the MDT. The view of the datasheet is our query also renders the data through this method.
Gets the data from the table tablename and returns it as an XML string. [Ajax.ajaxmethod ()] public string getxmlfindtable (string rowfilter) { DataTable DT=MDT; DataView Dv=dt.defaultview; Dv. Rowfilter=rowfilter; Return DB. Gettableouterhtml (DV); } |
Getxmlfindtable queries the data from the MDT by query criteria and returns it to the client.
Delete Row [Ajax.ajaxmethod ()] public bool DeleteRow (string Tablename,int ID) { String SQL = "Delete from" + tablename + "where id=" + ID; BOOL Issuccess= db. Db. ExecuteSQL (SQL); if (issuccess) { DataRow dr=mdt.select ("id=" +id) [0]; MDt.Rows.Remove (DR); } return issuccess; } |
DeleteRow is a row that deletes the physical table and is also deleted synchronously in the MDT.
Convert data rows to array returns [Ajax.ajaxmethod ()] public object Getdatarow (int iID) { DataRow dr=mdt.select ("id=" +iid) [0]; Return Dr. itemarray;//numeric type cannot be empty } Getdatarow is a row of the ID number IID from the memory table MDT that is returned to the client via an array. [Ajax.ajaxmethod ()] public string gettoolbartable (string tablename) { Return DB. Gettoolbartable (tablename); } |
Gettoolbartable is the content that is organized into toolbars to be returned to the client.
• Establish business data class Ttyupkdata. (see file TtyuPKData.cs) The namespace is Ttyu and the class name is Ttyupkdata.
There are some common methods in this class.
The public bool Insertdatarow (DataRow dr,int begincolumnindex) Inserts the data row Dr into the physical table, Begincolumnindex represents the insertion from the beginning column.
The public bool Updatedatarow (DataRow dr,int begincolumnindex,string Filter) Updates the data row Dr data to the physical table, Begincolumnindex indicates the column that started the update. The filter represents the row that you want to update. is a filter condition.
public string gettableouterhtml (DataView dv) is a common method of presenting a data view as a table. and define styles by class.
Gets the table header and all data from a data view, and returns the table string in XML format public string gettableouterhtml (DataView dv) { StringBuilder ret = new StringBuilder (); Ret. Append ("
");Ret. Append ("
Ret. Append ("
); Title foreach (System.Data.DataColumn dc in DV. Table.columns) { if (DC. ordinal==0)//The column is hidden Ret. Append (" |
"); Else Ret. Append (" |
" +DC). Columnname+ " | ");}Ret. Append ("
");This is the filtered data in the viewforeach (DataRowView DRV in DV){DataRow Dr=drv. Row;//dv. Table.rows[i];Ret. Append (getrowouterhtml (DR));}Ret. Append ("
"); return ret. ToString (); } |
• Create a page data editing class (see file PageEdit.cs) The namespace is ttyu.web and the class name is Pageedit.
This class is a common way to implement a unified approach to all editing pages, including adding and modifying data, through inheritance techniques. A line of code is not required on the page.
Conclusions
Ajax technology can provide a rich customer experience for clients, and Ajax.NET provides the possibility for you to achieve such powerful functionality easily. A static page does not appear to have a refresh problem. Our static pages can be fully integrated with ASP.net via Ajax. Manage in the background through. Net. The foreground is invoked through JavaScript. This perfect combination is the best way to solve the problem.
3. Scripting methods are written in JavaScript,
function Init () { Domtree=new Dhtmlxtreeobject (document.getElementById (' Divtree '), "100%", "100%", 0); Domtree.setimagepath ("imgs/"); Domtree.enabledraganddrop (True) Domtree.setdraghandler (Mydraghandler); Domtree.setonclickhandler (Selecttreenode); var s= ajaxdata.getxmltree (). value;
Domtree.loadxmlstring (s); Domtree.openitem ("r_1"); Selecttreenode ("r_1"); Divtoolbar.innerhtml=ajaxdata.gettoolbartable (tablename). Value; Divdatagrid.innerhtml=gettableouterhtml (tablename); } |
The Init method obtains the contents of the tree through the Getxmltree method of Ajax, Gettoolbartable gets the contents of the toolbar. The Gettableouterhtml method is used to obtain all the data of the physical table Tinfoclass and show it through the table.
function Find () { var table=ajaxdata.getxmlfindtable ("Grade name like '%" +txtname.value + "%"); Divdatagrid.innerhtml=table.value; } |
Query method Find () is an Ajax Getxmlfindtable method by grade name query and show the results.
function Openaddweb () { var retid =openaddwin (' infoclass_edit.aspx? Isadd=true ', 370,300); if (retid==-1) return; InsertRow (Datagrid1,retid); } |
The Openaddweb method opens the Infoclass_edit.aspx page to complete the add-data functionality on the page. If the return value is not 1, the new data is added and the data is inserted into the last row of the table. Retid represents the ID of the last line.
function Openeditweb (ThisCell) { var Id=thiscell.previoussibling.previoussibling.innertext; var retid =openaddwin ("infoclass_edit.aspx?id=" +id+ "&isadd=false", 370,300); if (retid==-1) return; There are update lines var currrow=thiscell.parentelement Setrowtext (Currrow); Setrowtext (Currrow,retdatarow); } |
The Openeditweb method is the ability to edit the row of data on the page after the user clicks the cell and opens the Infoclass_edit.aspx page. Replace the contents of the current row if there is an update line ...
• Create Ajax classes. (see file AjaxData.cs) The namespace is Ttyu and the class name is Ajaxdata.
1. Define the static variable MDT. MDT is a data table object that reads data from a physical table and holds it in memory. Our main operation is done by it. Defined as static is that we do not want to repeatedly access the database. We read it at once and use it permanently.
static public DataTable mDt; |
2. Define our main methods. Add [Ajax.ajaxmethod ()] to the uplink of each method.
Gets the data from the table tablename and returns it as an XML string. [Ajax.ajaxmethod ()] public string gettableouterhtml (string tablename) { DataTable dt=db. Db. Getdatatable (tablename); MDT=DT; Return DB. gettableouterhtml (dt. DefaultView); } |
The Gettableouterhtml method is read into memory by a physical table name and a table in XML format is obtained from its view. Here we read it and save it in the MDT. The view of the datasheet is our query also renders the data through this method.
Gets the data from the table tablename and returns it as an XML string. [Ajax.ajaxmethod ()] public string getxmlfindtable (string rowfilter) { DataTable DT=MDT; DataView Dv=dt.defaultview; Dv. Rowfilter=rowfilter; Return DB. Gettableouterhtml (DV); } |
Getxmlfindtable queries the data from the MDT by query criteria and returns it to the client.
Delete Row [Ajax.ajaxmethod ()] public bool DeleteRow (string Tablename,int ID) { String SQL = "Delete from" + tablename + "where id=" + ID; BOOL Issuccess= db. Db. ExecuteSQL (SQL); if (issuccess) { DataRow dr=mdt.select ("id=" +id) [0]; MDt.Rows.Remove (DR); } return issuccess; } |
DeleteRow is a row that deletes the physical table and is also deleted synchronously in the MDT.
Convert data rows to array returns [Ajax.ajaxmethod ()] public object Getdatarow (int iID) { DataRow dr=mdt.select ("id=" +iid) [0]; Return Dr. itemarray;//numeric type cannot be empty } Getdatarow is a row of the ID number IID from the memory table MDT that is returned to the client via an array. [Ajax.ajaxmethod ()] public string gettoolbartable (string tablename) { Return DB. Gettoolbartable (tablename); } |
Gettoolbartable is the content that is organized into toolbars to be returned to the client.
• Establish business data class Ttyupkdata. (see file TtyuPKData.cs) The namespace is Ttyu and the class name is Ttyupkdata.
There are some common methods in this class.
The public bool Insertdatarow (DataRow dr,int begincolumnindex) Inserts the data row Dr into the physical table, Begincolumnindex represents the insertion from the beginning column.
The public bool Updatedatarow (DataRow dr,int begincolumnindex,string Filter) Updates the data row Dr data to the physical table, Begincolumnindex indicates the column that started the update. The filter represents the row that you want to update. is a filter condition.
public string gettableouterhtml (DataView dv) is a common method of presenting a data view as a table. and define styles by class.
Gets the table header and all data from a data view, and returns the table string in XML format public string gettableouterhtml (DataView dv) { StringBuilder ret = new StringBuilder (); Ret. Append ("
");Ret. Append ("
Ret. Append ("
); Title foreach (System.Data.DataColumn dc in DV. Table.columns) { if (DC. ordinal==0)//The column is hidden Ret. Append (" |
"); Else Ret. Append (" |
" +DC). Columnname+ " | ");}Ret. Append ("
");This is the filtered data in the viewforeach (DataRowView DRV in DV){DataRow Dr=drv. Row;//dv. Table.rows[i];Ret. Append (getrowouterhtml (DR));}Ret. Append ("
"); return ret. ToString (); } |
• Create a page data editing class (see file PageEdit.cs) The namespace is ttyu.web and the class name is Pageedit.
This class is a common way to implement a unified approach to all editing pages, including adding and modifying data, through inheritance techniques. A line of code is not required on the page.
Conclusions
Ajax technology can provide a rich customer experience for clients, and Ajax.NET provides the possibility for you to achieve such powerful functionality easily. A static page does not appear to have a refresh problem. Our static pages can be fully integrated with ASP.net via Ajax. Manage in the background through. Net. The foreground is invoked through JavaScript. This perfect combination is the best way to solve the problem.
3. Scripting methods are written in JavaScript,
function Init () { Domtree=new Dhtmlxtreeobject (document.getElementById (' Divtree '), "100%", "100%", 0); Domtree.setimagepath ("imgs/"); Domtree.enabledraganddrop (True) Domtree.setdraghandler (Mydraghandler); Domtree.setonclickhandler (Selecttreenode); var s= ajaxdata.getxmltree (). value;
Domtree.loadxmlstring (s); Domtree.openitem ("r_1"); Selecttreenode ("r_1"); Divtoolbar.innerhtml=ajaxdata.gettoolbartable (tablename). Value; Divdatagrid.innerhtml=gettableouterhtml (tablename); } |
The Init method obtains the contents of the tree through the Getxmltree method of Ajax, Gettoolbartable gets the contents of the toolbar. The Gettableouterhtml method is used to obtain all the data of the physical table Tinfoclass and show it through the table.
function Find () { var table=ajaxdata.getxmlfindtable ("Grade name like '%" +txtname.value + "%"); Divdatagrid.innerhtml=table.value; } |
Query method Find () is an Ajax Getxmlfindtable method by grade name query and show the results.
function Openaddweb () { var retid =openaddwin (' infoclass_edit.aspx? Isadd=true ', 370,300); if (retid==-1) return; InsertRow (Datagrid1,retid); } |
The Openaddweb method opens the Infoclass_edit.aspx page to complete the add-data functionality on the page. If the return value is not 1, the new data is added and the data is inserted into the last row of the table. Retid represents the ID of the last line.
function Openeditweb (ThisCell) { var Id=thiscell.previoussibling.previoussibling.innertext; var retid =openaddwin ("infoclass_edit.aspx?id=" +id+ "&isadd=false", 370,300); if (retid==-1) return; There are update lines var currrow=thiscell.parentelement Setrowtext (Currrow); Setrowtext (Currrow,retdatarow); } |
The Openeditweb method is the ability to edit the row of data on the page after the user clicks the cell and opens the Infoclass_edit.aspx page. Replace the contents of the current row if there is an update line ...
• Create Ajax classes. (see file AjaxData.cs) The namespace is Ttyu and the class name is Ajaxdata.
1. Define the static variable MDT. MDT is a data table object that reads data from a physical table and holds it in memory. Our main operation is done by it. Defined as static is that we do not want to repeatedly access the database. We read it at once and use it permanently.
static public DataTable mDt; |
2. Define our main methods. Add [Ajax.ajaxmethod ()] to the uplink of each method.
Gets the data from the table tablename and returns it as an XML string. [Ajax.ajaxmethod ()] public string gettableouterhtml (string tablename) { DataTable dt=db. Db. Getdatatable (tablename); MDT=DT; Return DB. gettableouterhtml (dt. DefaultView); } |
The Gettableouterhtml method is read into memory by a physical table name and a table in XML format is obtained from its view. Here we read it and save it in the MDT. The view of the datasheet is our query also renders the data through this method.
Gets the data from the table tablename and returns it as an XML string. [Ajax.ajaxmethod ()] public string getxmlfindtable (string rowfilter) { DataTable DT=MDT; DataView Dv=dt.defaultview; Dv. Rowfilter=rowfilter; Return DB. Gettableouterhtml (DV); } |
Getxmlfindtable queries the data from the MDT by query criteria and returns it to the client.
Delete Row [Ajax.ajaxmethod ()] public bool DeleteRow (string Tablename,int ID) { String SQL = "Delete from" + tablename + "where id=" + ID; BOOL Issuccess= db. Db. ExecuteSQL (SQL); if (issuccess) { DataRow dr=mdt.select ("id=" +id) [0]; MDt.Rows.Remove (DR); } return issuccess; } |
DeleteRow is a row that deletes the physical table and is also deleted synchronously in the MDT.
Convert data rows to array returns [Ajax.ajaxmethod ()] public object Getdatarow (int iID) { DataRow dr=mdt.select ("id=" +iid) [0]; Return Dr. itemarray;//numeric type cannot be empty } Getdatarow is a row of the ID number IID from the memory table MDT that is returned to the client via an array. [Ajax.ajaxmethod ()] public string gettoolbartable (string tablename) { Return DB. Gettoolbartable (tablename); } |
Gettoolbartable is the content that is organized into toolbars to be returned to the client.
• Establish business data class Ttyupkdata. (see file TtyuPKData.cs) The namespace is Ttyu and the class name is Ttyupkdata.
There are some common methods in this class.
The public bool Insertdatarow (DataRow dr,int begincolumnindex) Inserts the data row Dr into the physical table, Begincolumnindex represents the insertion from the beginning column.
The public bool Updatedatarow (DataRow dr,int begincolumnindex,string Filter) Updates the data row Dr data to the physical table, Begincolumnindex indicates the column that started the update. The filter represents the row that you want to update. is a filter condition.
public string gettableouterhtml (DataView dv) is a common method of presenting a data view as a table. and define styles by class.
Gets the table header and all data from a data view, and returns the table string in XML format public string gettableouterhtml (DataView dv) { StringBuilder ret = new StringBuilder (); Ret. Append ("
");Ret. Append ("
Ret. Append ("
); Title foreach (System.Data.DataColumn dc in DV. Table.columns) { if (DC. ordinal==0)//The column is hidden Ret. Append (" |
"); Else Ret. Append (" |
" +DC). Columnname+ " | ");}Ret. Append ("
");This is the filtered data in the viewforeach (DataRowView DRV in DV){DataRow Dr=drv. Row;//dv. Table.rows[i];Ret. Append (getrowouterhtml (DR));}Ret. Append ("
"); return ret. ToString (); } |
• Create a page data editing class (see file PageEdit.cs) The namespace is ttyu.web and the class name is Pageedit.
This class is a common way to implement a unified approach to all editing pages, including adding and modifying data, through inheritance techniques. A line of code is not required on the page.
Conclusions
Ajax technology can provide a rich customer experience for clients, and Ajax.NET provides the possibility for you to achieve such powerful functionality easily. A static page does not appear to have a refresh problem. Our static pages can be fully integrated with ASP.net via Ajax. Manage in the background through. Net. The foreground is invoked through JavaScript. This perfect combination is the best way to solve the problem.