[Blog] The use jQuery with ASP.NET .ashx complete a simple Ajax

https://blog.csdn.net/yongh701/article/details/50481072

 

 

Disclaimer: This article is the original article bloggers, bloggers without permission, free to reprint welcome, a good standard of + original address on it! Thanks enjoy! Please back to a paste feel good! https://blog.csdn.net/yongh701/article/details/50481072
may be the same as in ASP.NET with other programming languages, the use of Ajax technology front desk, only to note that, backstage handler is no longer a aspx page Page_Load, and ASP.NET unique "general handler" .ashx, following a simple example to illustrate this problem.

Below, to a simple adder can use the program reception script completed. Here for illustrative purposes only.

 

First, the file structure of this solution in the following figure:

 

You must first right-click way to solutions, by way of adding "existing items" and a version of the high jQuery.js file is added to your solution, directly copy the file to copy the solution folder is useless , the pro-test.

 

Then, to add a "New Item", the new term is a general handler .ashx, rather than a general Web Form .aspx

 

Here, the generic handler, named Calculate.ashx, the following code, showing something to the front by context.Response.Write is done, the process for printing the content to the foreground.

Post here first accept two parameters passed to it from the front desk with context.Request.Form. If not empty, they are the result of adding print to the front.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ajax
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";//用于设置编码
string num1 = context.Request.Form["num1"];//如果是get方式应该用context.Request.QueryString
string num2 = context.Request.Form["num2"];
if (!(string.IsNullOrEmpty(num1) || string.IsNullOrEmpty(num2)))
{
context.Response.Write(int.Parse(num1) + int.Parse(num2));
}
}

public bool IsReusable // for thread safety
{
GET
{
return to false;
}
}
}
}

After completely Default.aspx HTML + javascript content reception, from the acquisition parameters of the two text boxes, which spread Calculate.ashx inside, and then print the results to the node id as the result of which span.
<% @ Page Language = "C #" the AutoEventWireup = "to true" CodeBehind = "Default.aspx.cs" the Inherits = "Ajax.Default"%>

! <DOCTYPE HTML the PUBLIC "- // the W3C // DTD XHTML 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<HTML xmlns =" the http://www.w3.org/1999/xhtml ">
<head ID = "Head1" the runat = "Server">
<title> Ajax </ title>
<Script type = "text / JavaScript" the src = "jQuery-1.8.3.js"> </ Script>
</ head>
<body>
< input type = "text" id = "num1" /> + <input type = "text"



. var num1 = $ ( "# num1") Val ();
var num2 = $ ( "# num2") Val ();.
IF (isNaN (num1) || isNaN (num2)) {
Alert ( "you entered not the number ");!
}
the else {
$ .ajax ({
type: 'POST',
URL: 'Calculate.ashx',
dataType:" HTML ",
Data: {
num1: num1,
num2: num2
},
Success: function ( Data) {
$ ( "# Result") HTML (Data);.
},
error: function () {
Alert ( "wrong. Please try again!!");
}
});
}
}
</ Script>
< / html>

See ASP.NET, jQuery and use .ashx complete a simple Ajax with other programming languages is almost no difference,
the only need is, Ajax background processing code must be written in .ashx file instead of Page_Load method .aspx If .aspx not been accessed, only preached parameters into account, it will not trigger Page_Load method.
---------------------
Author: yongh701
Source: CSDN
Original: https: //blog.csdn.net/yongh701/article/details/50481072
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/guoqiang1/p/10959180.html