In the released ASP. net2.0, the development of the brushless newest page has many changes with beta2. In addition, when more and more Ajax Development Kits are developed, ASP. net2.0's no-refreshing page technology is not widely known, and many people even think this feature is a little "chicken ". However, if we only add a few Ajax features to the program, Atlas, Ajax. net, and so on will seem a bit like a cool tool. In addition, I think it is not very complicated to use the methods provided by ASP. net2.0 for development. On the contrary, very little code can be used to achieve great results!
Next I will show you step by step how to develop a new page without refreshing!
Step 1: implement the icallbackeventhandler Interface
The icallbackeventhandler interface is located under the System. Web. UI namespace. In beta2, icallbackeventhandler only contains one raisecallbackevent method, that is, the callback event is processed and the processing result is returned. In the official version, it becomes a member method that includes getcallbackresult and raisecallbackevent. The first method is used to return the result of the callback event, and the second method is used to return the callback event. This change is mainly made to write Web controls. For details, refer to the implementation code in controls such as gridview.
Create a web site and modify the default. aspx. CS file:
The following is a reference clip: 1 public partial class _ default: system. Web. UI. Page, icallbackeventhandler 1 private string STR; 2 Public void raisecallbackevent (string eventargument) 3 { 4 // different processing logic can be called based on different passed parameters 5 STR = "content returned from the server:" + eventargument; 6} 7 8 Public String getcallbackresult () 9 { 10 return STR; 11} 12 |
Step 2: register the callback Method
We add a Textbox, a label, and an HTML control button on the default. aspx page, and add an onclick event to the button:
The following is a reference clip: 1 <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> 2 <input id = "button1" type = "button" value = "Submit to label1" onclick = "callserver (textbox1, label1)"/> <br/> 3 <asp: Label id = "label1" runat = "server" text = "label1:"> </ASP: Label> 4 |
The following is a reference clip: 1 <SCRIPT type = "text/JavaScript"> 2 // called by the button 3 function callserver (inputcontrol, context) 4 { 5 context. innerhtml = "loading "; 6 Arg = inputcontrol. value; 7 // register the callback Method 8 <% = clientscript. getcallbackeventreference (this, "Arg", "eseserverdata", "context") %>; 9} 10 // The function registered in the callback method to receive the returned results 11 function exploreserverdata (result, context) 12 { 13 context. innerhtml = result; 14} 15 </SCRIPT> 16 |
Now, the development of a refreshing page is complete. It can write the text you entered in the textbox back to the label of the page through the server code. Is it easy? You can run your program to see the effect!
Next we will analyze the code.
First, let's look
The following is a reference clip: <% = Clientscript. getcallbackeventreference (this, "Arg", "eseserverdata", "context") %>; |
Clientscript is an attribute of the system. Web. UI. Page object. It is a system. Web. UI. clientscriptmanager object. Used to manage client scripts. The getcallbackeventreference method is used to register client callbacks for a server event. Its fourth parameter "context" is very important, but there are no relevant examples and detailed descriptions in msdn. In the code above, we can see that the context parameter passed when calling the callserver method is label1, and the second parameter "context" of receiveserverdata is the transferred label1. In my example, context is used to set a control to display the results returned by the server. In fact, you can assign any object to the context, and it will be passed to the local end to process the callback returned result function. In this way, you can return results based on the specified "context" before the call! In my complete example, you can see an example of refreshing the display of the gridview using context.
Here I would like to say a digress. The important parameters such as context are not described in detail in msdn, but the callback example in the official Chinese version of vs2005 is still implemented in beta2! Msdn of this version can be said to be the worst-quality version I have used. However, the current msnd can be described as "rather large", and errors are inevitable. I hope the next version of msnd will be better.
OK. It is not difficult to develop something with Ajax features in ASP. NET 2.0! It is actually two steps:
1. Implement the icallbackeventhandler interface on the server. In the methods contained in the interface, call different processing methods based on the passed parameters, and then return results;
2. register the callback function on the client side (you can also register the callback function on the server side), and then implement the function to process the callback result. If you can flexibly run the context, you can achieve very good results.
In my complete example, you can see more complete processing logic and more beautiful special effects. Of course, the added code is still very small!
I haven't written a blog for nearly a year, and my hands are quite new. I used to read cnblogs articles on a daily basis. It's nice to read articles once in a week or two. Today, every day is for the purpose of life, people in the rivers and lakes, cannot help yourself!
After complaining for a long time, I also announced that I will write some articles about Atlas recently. In fact, many people have written about Atlas, but most of them are based on the official document model, however, the recent releases of several versions of CTP have changed a lot (The implementation functions are also quite attractive !), There are few articles about the new features, so I am sorry, I hope it will be helpful for you to use Atlas.