[Note: this paragraph has nothing to do with the title content. You can skip it] after reading the Stargate of the Two Sets and taking a bath, I finally decided to start writing this article. This is my first original technical article in the true sense. Regardless of the technical content, I have taken this step. In fact, there have been a lot of blogs, from the earliest campus bus, to the blog Park, and my Sina Blog, which is used for complaints. In fact, the initial purpose of blogging is to write programs at the same time, record your learning process. However, it has evolved into a whining thing. This is also the consequence that I have never been able to concentrate on. However, it is not too late for me to make up for it. From this article, I began to focus more on it, focus more on it. Guided by this kind of focus, I recently started to write programs well. This time, I really put down what I don't want to do, and started to write my favorite C # code with my mind, among the programming languages I know, I like C # and JavaScript most. The latter is a scripting language. To be precise, J is my favorite recently, I thought it was similar to the basic that I used to play when I was a child. It was small and simple, but it turns out not like this. Although it is also a script language like vbs, however, under the appearance of C language, more content is actually hidden. I may write another article to describe it. Now I like it. NET platform, I don't know why the first time I used C # To write code in, I thought it was very beautiful, and then I fell in love with it. But at that time, due to environment problems, so it was interrupted for a while and it was just 04 years before I picked it up again. But over a year, I was just pulling a pile of controls and then adjusting them in the property panel, it seems that I forgot what the web looks like, and then I finally got something to replace with my memories -------- Ajax .
Ajax development frameworks are everywhere, in fact, in ASP. when NET 2.0 was released, it actually integrated some similar content and was used on some data controls such as the gridview, in the msdn magazine article "Custom Script callback in ASP. net (Chinese Version | English) makes me realize the charm of asynchronous calls in Asp.net (the implementation method is only available in beta1, the script callback part beta1, beta2, and the official version are different. If you are interested, refer to the recently released msdn content, I didn't have much to worry about it, but some time ago, when a friend talked to me about Asp.net's Ajax implementation, it was too cumbersome to implement Atlas, however, what he wants to implement is that a few very small contents do not need to be so troublesome, because I have nothing but these. net Ajax framework is not familiar, so it naturally reminds me of the built-in script callback mechanism After his personal consent) for a friend's project, we wrote a lot of code about this. After writing it, I suddenly found a problem, that is, the code is too messy. Every page is similar, and only one string parameter can be passed, so that we have to use an embedded framework for some large data display in addition to interaction. I bought an "ajax advanced language programming" from the chart two days ago. After reading a part of it, I suddenly wanted to figure out how to implement the script callback in Asp.net 2.0? In fact, let's look back at the header, "Custom Script callback in ASP. in the article "Net", I have already explained a lot about it, but I can't help but understand the current level. Many things are in the fog of the cloud, the patronizer first posted the most concise implementation effect, and then analyzed it.
Create a default. aspx page, add a checkbox control on the page, open the default. aspx. CS file, and add three inherited interfaces icallbackcontainer, icallbackeventhandler, and inamingcontainer to the _ default class:
[Code 1]
# Region icallbackcontainer Member
Public String getcallbackscript (ibuttoncontrol buttoncontrol, string argument)
{
Throw new exception ("the method or operation is not implemented .");
}
# Endregion
# Region icallbackeventhandler Member
String temp;
Public String getcallbackresult ()
{
// Throw new exception ("sample error ");
Return temp;
}
Public void raisecallbackevent (string eventargument)
{
Temp = "_____" + eventargument + "is succeed ._____";
}
# Add the following code to the page_load method on the default. aspx. CS page:
[Code 2]
Proected void page_load (Object sender, eventargs E)
{
String temp = page. clientscript. getcallbackeventreference (this, "Arg", "Callback", "context", "onerror", true );
String script = "function callserver (ARG, context) {" + temp + "}";
Page. clientscript. registerclientscriptblock (this. GetType (), "ABC", script, true );
Checkbox1.attributes. Add ("onclick", "callserver ('I call Server', 'context ');");
}
After editing the CS code, open the default. aspx file and add the following code between [Code 3]
<SCRIPT type = "text/JavaScript">
Function onerror (ERR, context)
{
Alert (ERR );
}
Function callback (ARG, context)
{
Alert (ARG );
}
</SCRIPT>
Here, the checkbox control is pulled and can be changed as needed. However, when using the button control, you may need to note that it will activate the onsubmit event by default, therefore, you may need to set or directly use the HTML control. The above code is a minimal implementation of script callback, and is essential.
Controls that use basic callback must implement three interfaces: icallbackcontainer, inamingcontainer, and icallbackeventhandler. In fact, inamingcontainer does not have any interface content to implement. It only "identifies the Container Control for creating a new ID namespace in the control hierarchy of the Page Object" (derived from msdn ). As for the icallbackcontainer interface, the explanations provided in msdn (Chinese version) are vague. Some related articles also describe the script callback and icallbackeventhandler interfaces, because we use pages as the basis for callback, we didn't use the method getcallbackscript to implement this interface. However, if we encapsulate our Ajax controls, this method is very useful, here we only use the method implemented by icallbackeventhandler to process data, because in the page_load method, I registered a callserver method and then triggered it by The onclick event of the checkbox, in this way, we can view a clear call process.
Later [Code 3] I implemented two Javascript methods, one for handling call errors, and the other for processing returned information after successful calls. In [Code 1], I commented out a piece of code that throws an exception. Through this code, I can simulate the onerror method call.
Till now, we have seen how this call is implemented. To put it bluntly, this is actually a relatively advanced drag control method, but how is it implemented? Why can't I see anything about XMLHttpRequest? (I firmly believe this is the best way to implement Ajax, because this code can be used in any JavaScript browser. I think it should not be related to the dark door)
Compile and run .........
On the running page, click the multi-choice box to display "___ I call server is succeed .___". What if this is executed? In fact, you only need to click "view source code" on this page. There is a secret hidden here, and three script blocks are automatically generated on the page, one is _ dopostback, which is used to handle server control event sending back, the other is the callserer method we just registered using clientscript, And the link mark of an external script. This isKeyAnd the link URL points:
<SCRIPT src = "/techtest/webresource. axd? D = de9yrizlddq8oulo_3rqga2 & amp; T = 632919546726295408 "type =" text/JavaScript "> </SCRIPT>
Follow the SRC instructions above to open the address and obtain a webresource. axd file (enter the connection address in a download tool such as thunder to download it), open it and you will see that this file contains some JavaScript code: Try
{
Xmlrequest = new XMLHttpRequest ();
}
Catch (E)
{
Try
{
Xmlrequest = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (E)
{
}
}
Well-known code!
If (! Useasync)
{
If (_ synchronouscallbackindex! =-1)
{
_ Pendingcallbacks [_ synchronouscallbackindex] = NULL;
}
_ Synchronouscallbackindex = callbackindex;
}
In this? Isn't that the asynchronous call option in the page. clientscript. getcallbackeventreference method?
There are a lot of useful code in it. I am studying it .......................
In addition, there are many other methods. To put it bluntly, it is a very simple Ajax framework encapsulation. there are controls for processing events and sending back requests. On this basis, we can make other encapsulation, in this way, it can be directly encapsulated into a simple one. net Ajax control, as. net.
Java ,. net or PHP Technology is an advanced encapsulation of HTTP on the server side, just like the CGI technology we used a long time ago, and the current Web technology encapsulation is more advanced, but I learned about it. net internal operation mechanism. We can rewrite some pages or controls by ourselves to build our own Ajax development environment.
I used to be a control engineer. I think encapsulation is a necessary technology in the big factory era, but programmers still have to follow up on it, I really understand the content behind the program to better develop high-quality programs.