Asp.net uses Ajax pro to realize the interaction between front end and back end

Time:2021-6-2

Using Ajax Pro for interaction, many people have written an article, why should we continue the platitude. Because there are some details that we need to pay attention to, because if we don’t pay attention to these details, the program will report errors, and the maintenance is poor.

introduction

1、 First of all, it’s the same saying that if you want to practice magic skills, you must first go to the palace. The first step in developing a program is to build an environment.

Start visual studio, my version is 2012, but this does not affect. First create a website project, add bin directory to the project, and then add an index page.

Then, we need to go online to download Ajax pro.2.dll, baidu once a pile of links, here I don’t say much. Then copy or import the downloaded file of Ajax pro.2.dll toBinIn the directory. Then, in theWeb.config Of<system.web>Write the following under the tag.


<httpHandlers>
  <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>

And in the server background class, make the following modifications:


using AjaxPro;
//(1)

AjaxPro.Utility.RegisterTypeForAjax(typeof(Index));
//(2)

[AjaxPro.AjaxMethod]
//(3)

(1) : introducing Ajax Pro namespace

(2) : register class information to the front page

(3) : add this in front of the function to be called in each foreground page

Here I have an example. Pay attention to one point: in thePage_Load() Function, using a judgment. if(Page.IsCallback) What it does is itGets a value indicating whether the page request is the result of a callback. It’s a special return, so the round trip always happens; However, unlike traditional postbacks, script callbacks do not redraw the entire page. This is crawling from other bloggers. This is the original text. It’s also a small programming habit. I don’t know what the specific use is, but I just know it’s useful.

Above, let’s build the Ajax pro. 2 environment.

2、 Then, the call starts.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>AjaxPro.2.dll</title>
  <script>
    
    function show() {
      //(1)     
      var name = Index.getName().value;
      alert("My Name is :" + name);

    }

  </script>
</head>
<body>
  <form runat="server">
  <div>
    <input type="button" value="show" onclick="show()" />
  </div>
  </form>
</body>
</html>

Here, index is the class name of this class. I don’t know if I remember it. We have written such a piece of code Ajax pro. Utility. Register type for Ajax (type of (index)); The class name declared when configuring the environment. The front page also finds our class through this Value is also an important point. I remember the first time I used this plug-in. Return a password to judge whether the login is successful, but it always fails. This plug-in page uses log information, so I can only alert one by one. If you don’t use value here, you will get a lot of information that can’t be used in practical applications. Here you need to pay attention.

1) Think that this can be called successfully? It’s naive. Let’s talk about the first detail: changing the hosting pipeline mode of the project

Click the mouse to select this item, and then press F4 to display the properties of this item. Just change this property to the traditional one. If it is integrated, it will report 500.23

2) Another point is also a mistake. This error does not occur when compiling, nor when loading pages. Uncaught referenceerror: index is not defined only when calling Ajax pro. 2. When calling background code doesn’t work, you need to pay attention to whether it is this problem, because this error will not appear on the page and will not be compiled. If you want to see this error, you must debug it according to F12 page. I know that the error of Google Chrome browser is displayed in the console tab, and other browser bloggers are not particularly clear about it.

The reason for this error, ah, the blogger has also checked a lot of information, but there are few specific descriptions. Here bloggers can only do a summary of their own, if someone knows where there is a mistake, I hope you can actively correct (don’t mention it, make progress with each other. The blogger’s analysis is just because when building the environment, there is something wrong with it. Building environment bloggers have just made it clear in the previous article that if this error occurs, it should be 70% of the reasons.

3) This is also the detail that a blogger told me! He said this: when calling, you don’t have to use the declared class name. You can use the tag [Ajax namespace (“demo”)] to rename this class. The blogger gave it a try and it’s really OK. Put this tag in front of the called class, remember it is in front of the class. That’s it.

As you can see, the effect is achieved! This tag is very practical.

Thank you very much for watching, and I hope you can correct any mistakes mentioned above. Please comment and let me know. I will verify and revise it in time. I hope I will not mislead others. Thank you very much.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.