Download Sample Code 4 K
In the released
ASP. net2.0 , The development of the brushless newest page is the same Beta2 There are many changes. In addition, more and more Ajax When the development kit is developed, ASP. net2.0 The built-in brushless newest page technology has not been widely understood by many people, and even many people think that this function has some "chicken ribs ". But if we onlyProgramAdding a few Ajax Features, Atlas , Ajax. net And so on, it seems a little like "killing chicken with a knife. Besides, I think ASP. net2.0 The methods provided for development are not very complex. On the contrary, they are rarely used.CodeYou can achieve great results!
Next I will show you step by step how to develop a new page without refreshing!
Step 1: ImplementIcallbackeventhandler Interface
IcallbackeventhandlerInterfaceLocated inSystem. Web. UINamespace. InBeta2,IcallbackeventhandlerContains only oneRaisecallbackeventMethod, that is, processing the callback event and returning the processing result. In the official version, it is changed to includeGetcallbackresultAndRaisecallbackeventTwo member methods: the first is used to return the result of the callback event, and the second is used to return the callback event. This change is mainly for writingWebFor more information, seeGridviewControl.
Create Web Let's modify the website. Default. aspx. CS File:
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
In Default. aspx Add Textbox , One Label And one Html Widget Button And Button Add Onclick Event:1 <ASP: textboxID= "Textbox1"Runat= "Server"> </ASP: textbox>
2 <InputID= "Button1"Type= "Button"Value= "Submit to label1"Onclick= "Callserver (textbox1, label1 )"/> <BR/>
3 <ASP: LabelID= "Label1"Runat= "Server"Text= "Label1 :"> </ASP: Label>
4
1 < Script Type = "Text/JavaScript" >
2 // Called by button
3 Function Callserver (inputcontrol, context)
4 {
5 Context. innerhtml = " Loading " ;
6 ARG = Inputcontrol. value;
7 // Registration callback Method
8 <% = Clientscript. getcallbackeventreference ( This , " ARG " , " Receiveserverdata " , " Context " ) %> ;
9 }
10 // The function registered in the callback method to receive returned results.
11 Function Receiveserverdata (result, context)
12 {
13 Context. innerhtml = Result;
14 }
15 </ Script >
16
Now, a new page is developed, and you can Textbox Text entered in, written back to the page through the server code Label . Is it easy? You can run your program to see the effect!
Next we will analyze the code.
First, let's look<%=Clientscript. getcallbackeventreference (this,"ARG","Receiveserverdata","Context")%>;
clientscript Yes system. web. UI. page an attribute of the object, which is system. web. UI. clientscriptmanager object. Used to manage client scripts. The getcallbackeventreference method is used to register client callbacks for a server event. The fourth parameter " context is very important, however, no related examples and detailed descriptions are provided in msdn . The code above shows that when the callserver method is called, the context parameter is label1 , and receiveserverdata the second parameter " context "is passed 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 context , which will be passed to the local end for processing the callback returned results, in this way, you can return results based on the specified "context" before the call! In my complete example, you can see a context refreshing display gridview .
Here I want to talk about an external question,ContextSuch an important parameter isMsdnNot only does not have a detailed description, but alsoVs2005Chinese official versionMsdnThe callback example is stillBeta2Time! This versionMsdnIt can be said that the version I used has the worst quality. But nowMsndIt can be described as "rather large", and errors are inevitable. I hope the next versionMsndYes.
OK, InASP. Network 2.0WithAjaxIt's not hard to understand the features! It is actually two steps:
1,InServerTerminal ImplementationIcallbackeventhandlerInterfaceIn the methods included in the interface, different processing methods are called Based on the passed parameters, and then the results are returned;
2,InClientRegister the callback function (you can alsoServerAnd then implement the function to process the callback result. Where, ifContextCapable and flexible operation, you can make 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!
Not written for nearly a yearBlogNow, you have more hands. PreviousCnblogsOfArticleI have read it on a daily basis. It's nice to watch it once in a week or two, just a few articles. 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 will also give a notice that I will write some introductions recently.Atlas. ActuallyAtlasMany people have also written this article, but most of them are based on the official document model, and the latest releasesCTPThe changes are quite big (the functions are also quite attractive !), There are few articles about the new features, so I am sorry to use them.AtlasIt is helpful.