Creates a high-performance, lightweight webform framework that subverts your imagination-without refreshing the process of submitting a new background and returning parameters (the fifth day), webform framework
Question 5: When I use the aspx page to execute the background method, I always need to refresh the entire page? I want to submit a page in the background without refreshing a new page, and return parameters to execute the js script at the front end. That is to say, it works like ajax. What should we do?
Have you read the previous article? Let's summarize how to implement it?
First of all, we need to know that the execution background of our aspx page is submitted to the background through the form1 form by default. In the preceding <from> tag, there is an attribute called target, the system defaults to "-self". That is to say, the returned data points to itself, indicating that the entire page will be refreshed. If we do not want to refresh the entire page, we only need to place a hidden <iframe> label on the interface, and point the target attribute of the form with id = 'from1 'to the value of the name of <iframe>, then we will find that the page is not refreshed after the aspx page is executed in the background.
The above is a summary of my previous course !!! Next, let's take a look at C # How to transmit parameters to the foreground in the background and enable the foreground to receive parameters transmitted from the background ????
Actually, anyone who has written a general processing program knows how to pass parameters using Response. write ("I am a good guy"); this will input this value into iframe, but in iframe, we only need to input the value, however, we can see that in iframe, there are many elements we don't want in addition to the values we need, so we need to clear these elements. How can we clear them?
We can write it like this
Response. ClearContent (); Response. Write ("I am a good guy"); Response. End ();
As a result, the front-end iframe only displays the output value.
Now there is another problem. How can we determine the front-end to execute the front-end js method after the background c # code is executed?
I once thought about using settimeout to cyclically judge the value of the content in iframe. Although it can be implemented, it is easy to get stuck in an endless loop, and there is a time difference, the effect is far worse than ajax.
If you cannot do this, we can start from another aspect. We know that iframe has a load event. Here we must use the jquery event binding method, you need to remove the binding before using it, and then re-bind it. Otherwise, the iframe will not stop binding different load events. The code example is as follows:
$ ("# _ NofreshIframe"). unbind ("load ");
$ ("# _ NofreshIframe"). bind ("load", function () {alert (1 )});
Let's test whether the iframe load event can be executed after the background code execution is complete.
It is easy to obtain the value in iframe. Here we get the value in iframe,
Var result = $ (window. frames ["_ NofreshIframe" cmd.doc ument). text ();
This method is compatible with all versions of all browsers and cross-domain iframe issues.
In fact, after talking so much and talking so much nonsense, our ultimate goal is almost achieved. It is to create a method similar to ajax that can be transmitted to the background and return parameters, what we need to do is encapsulate the previous items similar to ajax post submission and look forward to future problems !!!!