[Note: This paragraph has nothing to do with the title content and can be skipped] After watching two episodes of Stargate and taking a shower, I finally decided to start writing this article. This is my first truly original technical article. Regardless of the technical content, I have finally taken this step. In fact, I have opened many blogs, from the earliest campus bus, to the Blog Park, and my Sina blog for complaining. In fact, the original purpose of starting a blog was to record my learning process while writing programs. . But later it evolved into something used to complain. This can be regarded as the consequence of my inability to concentrate on studying. However, as the saying goes, it is not too late to fix the problem. Starting from this article, I will become more focused and more focused. Guided by this focused thought, I finally started writing programs properly again recently. This time I really put down the things I didn’t want to do, and concentrated on writing the C# code I like. In the programming language I know, I My favorites are C# and Javascript. The latter is a scripting language. To be precise, I only fell in love with it recently. I originally thought it was just like the Basic I played when I was a child, small and simple, but it turns out that it is not like this. It looks like, although it is also a scripting language like VBS, there is actually more content hidden under the appearance of C language. I may write another article to describe it. Now let’s get to the point. I like it. .NET platform, I don’t know why when I first used C# language to write code in 2001, I thought it was very beautiful, and then I fell in love with it. However, due to environmental problems at that time, it was interrupted for a while until 2004. , I picked it up again, but for more than a year, I was just pulling piles of controls, and then adjusting them in the property panel. It seemed that I had forgotten what the Web originally looked like, and then finally There is something that has changed my memories---- Ajax.
Ajax development frameworks are everywhere. In fact, when ASP.NET 2.0 was released, some similar content was integrated internally. It is used in some data controls such as GridView. In an article in MSDN Magazine in January 2005, " Custom Script Callback in ASP.NET" (Chinese version | English) made me realize the charm of asynchronous calls in asp.net (the implementation method is limited to the beta1 version, about the script callback part beta1, beta2 and the official version They are different from time to time. Those who are interested can refer to the recently released MSDN content for themselves), but I just played with it at the time. Later, I used Atlas, so I didn’t pay much attention to it. However, a friend discussed ASP with me some time ago. When implementing ajax in .net, I mentioned that the implementation of atlas is too cumbersome, and what he wants to implement is only a few very small contents that don’t need to be that troublesome. Since I am not familiar with the ajax framework of .NET other than these, I naturally I remembered the built-in script callback mechanism. Using a friend's project (with his own consent), we wrote a lot of code about this. After writing it, I suddenly discovered a problem, that is, the code was too messy. Each page is similar, and only one string parameter can be passed. In addition to interaction, we have to use inline frames to display some expensive data. Two days ago I went to Haitu and bought a book "Ajax Advanced Language Programming". After reading part of it, I suddenly wanted to understand how to implement the script callback in asp.net 2.0? In fact, looking back now, many places in the article "Custom Script Callback in ASP.NET" have been explained very clearly. It can be said that at the time, many things were in the clouds, and patrons can only see the effect. I will post what I think is the simplest implementation first, and then analyze it.
Create a new Default.aspx page, add a CheckBox control on the page, and then open the Default.aspx.cs file. The _Default class adds three inherited interfaces, ICallbackContainer, ICallbackEventHandler and INamingContainer:
[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 members
string temp;
public string GetCallbackResult()
{
//throw new Exception("Sample Error");
return temp;
}
public void RaiseCallbackEvent(string eventArgument)
{
temp = "_____" + eventArgument + " is succeed._____";
}
#endregion
Go to the Page_Load method of the Default.aspx.cs page and add the following code:
[Code 2]
protected 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 <head>...</head>:
[Code 3]
<script type="text/javascript">
function OnError(err,context)
{
alert(err);
}
function Callback(arg,context)
{
alert(arg);
}
</script>
The CheckBox control here can be pulled up casually and can be changed if necessary. However, you may want to pay attention when using the Button control. It will activate the OnSubmit event by default, so you may need to set it up or use the HTML control directly. The above code can be regarded as a minimal implementation of script callback, and they are all necessary.
Controls that use basic callbacks must implement three interfaces: ICallbackContainer, INamingContainer and ICallbackEventHandler. In fact, INamingContainer has no interface content that needs to be implemented. It just "identifies the container control that creates a new ID namespace within the control hierarchy of the Page object" (quoted from MSDN). As for the ICallbackContainer interface, the explanation given in MSDN (Chinese version) is relatively vague. Some related articles also introduce script callbacks and ICallbackEventHandler interfaces. Because we use pages as the basis for callbacks here, we do not use this. The method to be implemented by the interface is GetCallbackScript, but if you are encapsulating your own Ajax control, this method is very useful. Here we just use the method implemented by ICallbackEventHandler to process data, because in the Page_Load method I registered a CallServer method, and then Triggered on the OnClick event attached to the CheckBox, so that we can understand a clearer calling process.
Later [Code 3] I implemented two Javascript methods, one is used to handle call errors, and the other is used to handle the return information after the call is successful. In [Code 1], there is a piece of code that throws an exception that I commented out. Through this code, I can simulate calling the OnError method.
Until here we have seen how to implement this call. To put it bluntly, this is actually a relatively advanced way of dragging controls, but how is it implemented? Why don't I see anything about XmlHttpRequest? (I firmly believe that this is the best way to implement Ajax, because this code can be used on any browser that supports Javascript. I think it has nothing to do with the secret door)
Compile, run......
Clicking the checkbox on the running page will display "___I call Server is succeed.___". How is this executed? In fact, just click "View Source Code" on this page. There is a little secret hidden here. Three script blocks are automatically generated on the page. One is __doPostback, which is used to handle server control event postback. , the other one is the CallSerer method we just registered using ClientScript, and there is also a link tag for an external script. This is the key. The URL it connects to is:
<script src="/TechTest/WebResource.axd?d=DE9YrizlDDq8OUlo_3rQgA2&t=632919546726295408" type="text/javascript"></script>
Open the address according to the address indicated by the SRC above, and you can get a WebResource.axd file ( Fill in the connection address into a download tool such as Thunder and you can download it). When you open it, you can see that this file actually contains some Javascript code:
try
{
xmlRequest = new XMLHttpRequest();
}
catch(e)
{
try
{
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
}
}
The code looks so familiar!
if (!useAsync)
{
if (__synchronousCallBackIndex != -1)
{
__pendingCallbacks[__synchronousCallBackIndex] = null;
}
__synchronousCallBackIndex = callbackIndex;
}
Inside this? Isn't this what handles the asynchronous call option in the Page.ClientScript.GetCallbackEventReference method?
There are a lot of useful codes in it. I am researching.........................
There are many methods in addition to these. To put it bluntly, it is a very simple Ajax The encapsulation of the framework includes processing control events and processing postback requests. We can make other encapsulations on this basis, so that it can be directly encapsulated into a simple .NET Ajax control as a component of .NET during development. Lightweight solution.
In fact, Java, .NET or PHP technology are all advanced encapsulation of HTTP on the server side, just like the CGI technology we used a long time ago, and now the technology of the Web is more advanced, and understanding When it comes to the internal operating mechanism of .NET, we can break away from some restrictions and rewrite some pages or controls ourselves to build our own Ajax development environment.
I used to be a control engineer. I think encapsulation is a necessary technology in the era of big factories. However, programmers still need to dig deeper and truly understand the content behind the operation of the program so that they can better develop high-quality products. program.