Refresh | no Refresh | page
In the published asp.net2.0, no refresh page development related parts and Beta2 have a lot of changes. And in more and more Ajax development package is developed in the case, asp.net2.0 with no Refresh page technology is not a lot of people understand, and even many people think that the function of some "chicken." But if we just add a few Ajax features to the program, Atlas, Ajax.NET, and so on, look like "overkill." Also, I think it's not very complicated to use the method provided by asp.net2.0, instead, using very little code can make a great effect!
Below I come step-by-step with you to develop the page without refreshing!
First step: Implement ICallbackEventHandler interface
The ICallbackEventHandler interface is located under the System.Web.UI namespace. When Beta2, ICallbackEventHandler contains only one raisecallbackevent method, which handles the callback event and returns the processing result. In the official edition, it becomes the GetCallbackResult and RaiseCallbackEvent two member method, the first to return the result of the callback event, and the second to come out to callback the event. This change is mainly for the purpose of writing Web control changes, you can look at the GridView and other controls in the implementation code.
To create a Web site, we modify the 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//can invoke different processing logic depending on the parameters passed
5 str = "Content returned from server side:" + eventargument;
6}
7
8 public string GetCallbackResult ()
9 {
Ten return str;
11}
12
Step Two: Register callback method
We add a TextBox, a Label and an Html control button to the Default.aspx page and add the OnClick event to the button:
1 <asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
2 <input id= "Button1" type= "button" value= "Submit to Label1"/><br/>
3 <asp:label id= "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 the returned result
One function Receiveserverdata (result, context)
12 {
context.innerhtml = result;
14}
</script>
16
Well, a page without a refresh has been developed, and it can write the text you enter in the TextBox back to the Label of the page through the server code. Is it simple? You can run your program to see the effect!
Let's analyze the code below.
First, let's look at
<%= Clientscript.getcallbackeventreference (This, "Arg", "receiveserverdata", "context")%>;
ClientScript is an attribute of the System.Web.UI.Page object, which is a System.Web.UI.ClientScriptManager object. For managing client script, the GetCallbackEventReference method is used to register a client callback for a server-side event. Its fourth parameter "context" is very important, but there are no relevant examples and detailed instructions in MSDN. As I can see in the code above, when the CallServer method is invoked, the context parameter passed is Label1, and Receiveserverdata's second parameter "context" is the Label1 that is passed over. In my example, the context is used by me to set up a control that displays the results returned by the server. In fact, you can assign any object to the context, and it will be passed to the local side to process the callback to return the result, so that you can return the result based on the "contextual" flexibility you specified before the call! In the complete example I gave, You can see an example of a no refresh display GridView using the context.
Here I would like to say a digression, the context of such an important parameter in MSDN not only not detailed description, and VS2005 Chinese official version of MSDN in the case of callbacks is still beta2 when the implementation! This version of MSDN can be said to be the worst of the versions I've used. But now the msnd can use "quite" large to describe, error is inevitable, hope that the next version of the msnd will be better.
OK, it's not hard to develop AJAX features in ASP.net 2.0! It's actually two steps:
1. Implement the ICallbackEventHandler interface on the server side, call different processing methods according to the parameters of the interface, and then return the result;
2. Register the callback function on the client side (you can also register on the server side) and implement the function that handles the callback result. Among them, if the context is able to operate flexibly, you can make a very good effect.
In the complete example that I have given, you can see more complete processing logic and more beautiful effects, of course, the added code is still very small!
Nearly a year has not written a blog, the hand is also relatively born. Previously Cnblogs article I was basically every day to see, now one or two weeks to see once a good, or a cursory glance of several. Now every day is to live and rush, people in the rivers and lakes, involuntarily ah!
I've been complaining for a while and I'm going to make a few more recent articles about Atlas. In fact, Atlas also has a lot of people have written, but mostly according to the official document model written, and recently released a few version of the CTP changes quite large (the implementation of the function is also more attractive!) There are fewer articles on the new features, so I came to shortcoming to help you with Atlas.