Jquery+ajax+c# A simple example of implementing database data without refreshing operation _jquery

Source: Internet
Author: User
Tags html tags

We know the difference between synchronous execution and asynchronous execution, in order to better improve the user experience, we will use asynchronous way to deal with some problems, after all, single-threaded synchronization may cause card Deng phenomenon, very unfriendly, so you can use Ajax to complete the user experience, Now let's talk about using jquery Ajax to get content without refreshing.

We are only a one-sided access to content, pagination, etc. will not be considered, later on no refresh of the paging

Page we place a div container to store the returned content

<div id= "comment" >
</div>

Note: Wait.gif is a similar progress bar effect, when not loaded content is not loaded out of the time, it shows this effect, so that can improve the user experience. And the most important thing is jquery, which needs Ajax to do.

Code:

Copy Code code as follows:

function GetInfo ()
{
$.ajax ({
URL: "Doaction.aspx?fig=reader&id=1&page=1",
Type: "POST",
Success:function (Data)
{
$ ("#comment"). HTML (Data);
Receiving via Arguments[0] can also be $ ("#comment"). html (arguments[0]);
},
Error:function ()
{
Alert ("program error");
}
})
}

URL: url to point to

Type: The method of submitting, which can be post or get

Success: function to execute after communication succeeds

Error: function to execute after communication failed

Beforesend: Han Yu to execute before communication

Complete: function to execute after communication completes

The post submission method is more secure than get, so you can replace it with the following method

Copy Code code as follows:

function GetInfo1 () {
$.post ("doaction.aspx", {fig: "Reader", ID: "1", Page: "1"}, function () {
$ ("#comment"). html (arguments[0]);
})
}

You can see that the parameters are written in a different way, using post submission, the first and second way is the difference between:

First of all, when the parameters are received, the first way in the Doaction.aspx page is to use request.querystring["fig"] to receive, the second way is to use the request.form["fig"]

Second, the first way when communication fails, you can return the error message in a friendly way, and the second method can be returned without notice.

Then in the Doaction.aspx page, as long as the corresponding processing can be

Copy Code code as follows:

if (request.form["fig"]!= null && request.form["Fig"]. ToString () = "Reader")
{
Ajax_getcomment ("1", 1);
}
Getting data from the database
private void Ajax_getcomment (string id, int page)
{

using (commentbo cm = new Commentbo (ID, page-1))
{
Response.Write (Cm.getcommentcontent ());
}
}


Also, using $.get ("", {},function () {}) is the same if you use the Submit method of get.

Another way to load content is to make the load () method, which can be used to refer to the API, to simply say how load is loaded with the load

Code

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

    < Title></title>
    <script src= "js/jquery.js" type= "Text/javascript" ></script
    <script type= "Text/javascript"
 $ (function () {
      $ ("#btnLoad"). Click (function () {
  $ ("#content"). Load ("doaction.aspx", {fig: "Reader", ID: "1", page: "1 "},function (ResponseText, Textstatus, XMLHttpRequest) {
      alert (responsetext);
 });
    })
 })
    </script>
<body>
    <form id= "Form1" runat= "Server"
    <input type= "button" id= "Btnload" value= "load Load"/>
     <div id= "content"

</div>
</form>
</body>


So the content is loaded into the page, there is a problem is found that the content returned in the page and alert pop-up some of the different, alert pop-up return HTML tags, and the page does not, that is because the page has parsed the HTML code

Other additions, modifications, deletions and other basic operations are also so, much the same, but only in the Doaction.aspx page to invoke different methods, the following is the main jquery code posted out to see

Copy Code code as follows:

function Addpinlun ()
{
var $CommentParentID =arguments[0];
var $CommentUser =$ (' #CommentUser '). Val ();
var $CommentText =$ (' #CommentText '). Val ();
var $CommentValidate =$ (' #CommentValidate '). Val ();

if ($.trim ($CommentUser) = = ")
{
Alert ("Please fill in the nickname!") ");
$ ("#CommentUser"). focus ();
return false;
}
if ($.trim ($CommentText) = = ")
{
Alert ("Please fill in the comments!") ");
$ ("#CommentText"). focus ();
return false;
}
if ($.trim ($CommentValidate) = = ")
{
Alert ("Please enter a verification code");
$ ("#CommentValidate"). focus ();
return false;
}
Stopbutton (' Commentsubmit ', 10);
Verify completion can be added by Ajax
$.ajax ({
URL: "doaction.aspx?action=add&commentparentid=" + $CommentParentID + "&commentuser=" + Escape ($CommentUser) + "&commenttext=" + Escape ($CommentText) + "&commentvalidate=" + Escape ($CommentValidate) + "&time=" + new Date (). ToString (),
Type: "Get",
Success:function (data) {
if (arguments[0] = = "ERROR") {
Alert ("Verify code timed out, please re-enter");
}
else {
GetInfo ();
$ ("#CommentText"). Val ("");
Refresh the verification code picture when validation succeeds
$ ("#CommentValidateImages"). attr ("src", "verifycode.aspx?s=" + math.random ());
Alert ("Add success");
$ ("#alertMessgae"). HTML (data);
}
$ (' #CommentValidate '). Val ("");
}
})
}

function Stopbutton ()
{
document.getElementById (Arguments[0]). Disabled=true;
document.getElementById (Arguments[0]). Value= "Submitted (" +arguments[1]+ ")";
if (arguments[1]>=1)
{
arguments[1]--;
Window.settimeout ("Stopbutton" ("+arguments[0]+", "+arguments[1]+") ", 1000);
}
Else
{
document.getElementById (Arguments[0]). Disabled=false;
document.getElementById (Arguments[0]). value= "Submit";
}
}


Doaction.aspx part of the code page of the page
Copy Code code as follows:

if (request.querystring["action"]!=null && request.querystring["action"]== "add")
{
if (session["Verifycode"]. ToString (). ToLower ()!= commentvalidate. ToLower ())
{
Response.Write ("ERROR");
}
Else
{
Dbquery.executescalar ("INSERT into comment (Commentparentid,commentuser,commenttext,commentreply,commentip) values (' + Commentparentid + ', ' "+ Commentuser +" ', ' "+ Server.HTMLEncode (commenttext) +" ', ', ' "+ request.servervariables[ "REMOTE_ADDR"] + "')");
Response.Write ("<script>alert!</script>");
}
}

Ajax in a lot of properties, you can view the API, according to their own needs to invoke different properties on it, there is one thing to note

If you judge whether the user name is duplicated, use $.getifmodified instead of $.get, you can test

If you use $.get for the first time you register a name, you do not do other operations, you will be in the text box to enter the same name, that time can also register, this should be noted.

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.