Here are some points. We use Asp.net to develop database applications. You often need to create entities such
Public class userentity
...{
Public String Username
...{
Get ...{
Return "";}
}
Public String useremail
...{
Get ...{
Return "";}
}
}
The following describes how to use scripts to simulate such entities.
First of all, we need to explain the concept of returning such entity scripts.
Userentity = function ()
...{
Return... {"username": "shenyi", "useremail": "shenyi@csdn.net "}
}
In this way, we only need to write
VaR scriptentity = new userentity ();
Scriptentity. Username;
Scriptentity. useremail;
It can be referenced, and the script entity has been built. Next we will retrieve the data from the server as follows:
Create An ASPX page. If getdata. aspx is called, write it in the code.
Public void page_load ()
...{
Int userid = int. parse (request. querystring ["uid"]. tostring ());
String SQL = "select * from users where userid =" + userid. tostring ();
Using (datatable dt = sqlhelper. executedataset (SQL). Tables [0]) // sqlhelper do not tell me what it is
...{
// Construct a character here to make a script variable equal to {"username": DT. Rows [0] ["username] ......}
// Such as response. Write ("userreturnvalue = {... // splice yourself here }");
Response. End ();
}
}
The following is how to establish a connection between the script and the ASPX page:
VaR userreturnvalue = "" // This is very important because it must be assigned a value on the server.
Comjs = function ()
...{
This. getentitying = function (UID, callback)
...{
VaR scriptobj = Document. createelement ("script ");
Scriptobj. type = "text/JavaScript ";
Scriptobj. src = "getdata. aspx? Uid = "+ uid
Document. getelementsbytagname ("head") [0]. appendchild (scriptobj );
Scriptobj. onreadystatechange = function ()
...{
If (scriptobj. readystate = "loaded ")
...{
Callback. endgetentity (userreturnvalue );
}
}
}
}
Using callback is the most important step. After the script is loaded, it will assign values to userreturnvalue. Therefore, we cannot directly obtain this object.
After the above script is written, we will start to obtain this object on the page.
VaR CJS = new comjs (); // initialize our script object
Function begingetentity ()
...{
This. endgetentity = function (entitydatalist)
...{
// Entitydatalist: this is the final object. You can directly use entitydatalist. username or entitydatalist. Field name in the table.
// Obtain the object of this object
// Note that business operations must be performed in this function. This is a callback. That is, execution starts only when the server obtains the data here.
Strictly speaking, data is obtained asynchronously. Similar to Ajax, but does not use XMLHTTP objects
}
}
VaR Bge = new begingetentity ();
CJS. getentity ("3", bgelist); // here 3 indicates that I want to retrieve data whose user ID is 3
Good/the whole method of building entities using scripts is well written. I hope it will be useful to everyone, especially beginners. In fact, to implement Ajax in scripts, not only XMLHTTP can implement very common scripts, but also
Please follow my personal website: www.shenyisyn.org