Preface
There were not many experiences with generating callback Javascript in XMLHttpRequest,
When the program was started, it was updated by Ajax. Net updatepanel.
I learned that updatepanel is just a batch of Ajax, And the postpaid effort is still relatively large,
I want to understand how Ajax creates tokens.
JavaScript and Ajax capabilities must be achieved if you want to upgrade your autopostback to a large cost.
This example works with jquery's Ajax function to show how it works on ASP. NET.
Play it
In this example, we need to have two Textbox, one being ID and the other being name,
When the ingress ID changes, the information with the name is required. (After the meeting, you can adjust autopostback = true, text_changed ...)
Here I handle the program with the ID and name in three different cases.
- . Ashx (generic processing regular)
- . Aspx (general webpage)
- . Asmx (Web Service)
In order to facilitate you to run the sample code directly,
The name in this example is the first name, followed by the second response data middlename: "Middle" returned by Js,
The part of the last name is whether the session value of the reset session can be obtained by the server. Therefore, the session value of the reset session is "91 』 Laixiangnian Indicates the name of the restore from the server.
That is, the Ajax return value is: first name + "Middle" + session ["lastname"]
Next, let's take a look.
- . Ashx
- First, add a new generic processing regular case. Remember to reference the system. Web. sessionstate and the actual ireadonlysessionstate so that we can upload the session in the program kernel.
- In the response data section, use context. Request. Form ["response data name]
- Use context. response. Write (return value)
- . Aspx
- First Add a new. aspx, and then only keep the row of the first line <% @ Page %>.
- Compile the program to be processed in the page_load event
- Request. Form ["response data name]
- The final return value is directly response. Write (return response value)
- . Asmx
- First Add a new. asmx, and then cancel the [system. Web. Script. Services. scriptservice] solution.
- Add a public method called getfullname. Add [webmethod (enablesession = true)] to the method header to use the session.
- The return information can be directly returned. Although the return type is not string, JavaScript returns strings in XML format.
With these three processing IDs,
Let's take a look at how we make up our ideas.
The part of the website is used as an example in three groups:
View Source
Print?
02 |
<form id="form1" runat="server"> |
04 |
.ashx: first name<asp:TextBox ID="txtIDbyAshx" runat="server"></asp:TextBox> |
05 |
full name<asp:TextBox ID="txtNamebyAshx" runat="server"></asp:TextBox><br /> |
07 |
.aspx: first name<asp:TextBox ID="txtIDbyAspx" runat="server"></asp:TextBox> |
08 |
full name<asp:TextBox ID="txtNamebyAspx" runat="server"></asp:TextBox><br /> |
10 |
.ashx: first name<asp:TextBox ID="txtIDbyAsmx" runat="server"></asp:TextBox> |
11 |
full name<asp:TextBox ID="txtNamebyAsmx" runat="server"></asp:TextBox><br /> |
Next, use the Ajax function of jquery. Note the following:
- Zookeeper in the change event
- Ajax option:
- Type: Use post
- In the URL section, the Web service must be added with the method name Signature
- Returns the part of the input data, which can be JSON or a string connected by a string like a post or querystring.
- If Ajax is successful, it will enter success, and the loss will result in an error.
- String of successful callback, In the success function (return callback value)
- The Web Service is returned in XML format and must be used by parse.
Conclusion
The ability of Ajax has not come to be powerful, because no matter whether it is common HTML, complex web form, or ASP. net mvc,
Waiting for the post process is a kind of torment for the user.
Since there are not many tips, let's learn it!