I. Goals
I wanted to improve a small function on the website background yesterday.UserInUserAfter you enter the file name, the system automatically performs the verification without refreshing the file. It has simple functions, but it has encountered unexpected troubles. The following is a preview of the effect:
Ii. Knowledge Review
First, let's take a look.Jquery AjaxThe most common basic method. You can skip this section.
1.Jquery. Get (URL, [data], [callback]):UseGetAsynchronous request
Code
Parameters:
URL (string): the URL of the request.
Data (MAP): (optional) data to be sent to the server, with the key / The key-value pair of value, which is appended to the request URL as querystring.
Callback (function): (optional) callback function when the load is successful (this method is called only when the response returns success ).
2. Jquery. Post (URL, [data], [callback], [type]):UsePostAsynchronous request
Code
Parameters:
URL (string): the URL of the request.
Data (MAP): (optional) data to be sent to the server, with the key / Key-value pairs of values.
Callback (function): (optional) callback function when the load is successful (this method is called only when the response returns success ).
Type (string): (optional) the official description is: type of data to be sent. In fact, it should be the client request type (JSON, XML, and so on)
Iii. niudaoTest
Page design: the main control is a textbox input file name and the label control displays the verification result.
< ASP: textbox Runat = "Server" ID = "Txtname" Cssclass = "Adminselect06" > </ ASP: textbox >
< BR />
< Label ID = "Tipname" Class = "Admintext14" > </ Label >
Background judgment:
Add pageCheckfilename. aspx, This isAjaxPage of the request, and return the result to the home page. Its,Background FunctionsCodeMainly as follows:
Add
Dbconn DB = New Dbconn ();
String Name = "" ;
Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{
// 1. first obtain the parameters passed in by the Ajax Method
If (Request. querystring [ " Name " ] = Null | Request. querystring [ " Name " ]. Tostring (). Trim () = "" )
{
MessageBox. Show ( " Enter the video name " );
Return ;
}
Else
Name = Request. querystring [ " Name " ]. Tostring ();
// 2. perform database operations to check whether the file name is repeated,
Filesame = New Filesame (); // Custom Enumeration type to store detection results
String SQL = " . Select ...... " ;
Datatable dt = DB. getdataset (SQL, " File " ). Tables [ 0 ];
If (Dt. Rows. Count > 0 )
Filesame = Filesame. allsame;
Else Filesame = Filesame. onlycontent;
// 3. Return the corresponding text based on the detection result
Switch (Filesame)
{
Case Filesame. notsame:
Response. Write ( " * The file with the same name does not exist. " );
Break ;
Case Filesame. Same:
Response. Write ( " * An object with the same name already exists. " );
Break ;
}
}
}
ForegroundJqueryMethod to implement the verification of refreshing:
< Script SRC = " JS/jquery-1.4.2.js " Type = " Text/JavaScript " > < / SCRIPT>
< Script Language = " Javascript " Type = " Text/JavaScript " >
$ (Document). Ready (
Function (){
$ ( " # Txtname " ). Blur ( Function (){
VaR Name = $ ( " # Txtname " ). Val ();
$. Get ( " Checkfilename. aspx? Name = " + Name, Null , Function Callback (data) {$ ( " # Tipname " Pai.html (data );});
})
})
< / SCRIPT>
4. Trouble:
Trouble 1 :.ProgramThe following errors are continuously prompted during execution (Invalid character in a base-64 string).
I checked some information. The reason should be that there is a special character "*. Because it can only contain0-9,A-Z, A-ZIs valid.After adjustment, we finally achieved the desired effect.
Trouble 2:However, another problem was found during the operation. That is, an error occurs when you click any server control on the page (The State information is invalid for this page and might be upted.).
Finally, we found a solution:
First findAjaxIn the requested fileResponseSome code, that is.
Switch (Filesame)
{
Case Filesame. notsame:
Response. Write ( " * The file with the same name does not exist. " );
Break ;
Case Filesame. Same:
Response. Write ( " * An object with the same name already exists. " );
Break ;
}
Then addResponse. End ()You can.
Switch (Filesame)
{
Case Filesame. notsame:
Response. Write ( " * The file with the same name does not exist. " );
Response. End ();
Break ;
Case Filesame. Same:
Response. Write ( " * An object with the same name already exists. " );
Response. End ();
Break ;
}< P>
This is the final solution. The Research on jquery is not very deep . I only want to share some of my experiences with you.