The principle of ASP.net script callback out of encapsulation analysis

Source: Internet
Author: User
Asp.net| Encapsulation | script

[Note: This paragraph has nothing to do with the title content, but Skip] after reading the two episode Stargate and taking a bath, I finally decided to start writing this article. This is my first real original technical article, no matter how technical content, I have taken this step. Blog actually opened a lot, from the earliest campus bus, to the blog park, as well as I used to Grumble Sina blog, in fact, the original purpose of opening a blog is to write the program at the same time, the record of their learning process. But it turned out to be something to whine about, which is what I've never been able to concentrate on, but it's not too late to do it, just starting with this article, becoming more focused and focused. Guided by this kind of attentive thought, I recently finally started to write a good program, this time is really put down the things that do not want to do, concentrate on the start to write my favorite C # code, in the programming language I know my favorite is C # and JavaScript, the latter is a scripting language, J is exactly what I liked recently, I thought it was the same as when I was a kid playing basic, small and simple, but it turns out that is not the case, although it is the same as the VBS script language, but in its C-language appearance, actually hiding more content, This I might write another article to describe it, now cut to the chase, I like. NET platform, do not know why 01 years when the first time using the C # language to write code, I think it is very beautiful, and then fell in love with him, but at that time due to environmental problems, so once interrupted, until 04 years, and then pick up again, but more than a year, I was just pulling a pile of control, Then in the property panel adjustment Ah, seems to forget the web is what the original look like, and then finally something changed my memory--------Ajax.

There is an AJAX development framework everywhere, in fact, when the ASP.net 2.0 release of the internal integration of a number of similar content, in some data control such as the GridView on the use, in the January 05 MSDN Magazine, a "Custom Script Callback in asp.net "(Chinese version | Chinese) has made me realize the charm of the asynchronous invocation within ASP.net (the implementation method is limited to the implementation in the Beta1 version, and there are differences between the script callback section beta1, Beta2, and the official version, and interested people can see the most recently published MSDN content), But it was just a bit of a game, and then using Atlas, it didn't really matter much, But a while ago a friend and I talked about ASP.net implementing Ajax, the Atlas implementation is too cumbersome, and he wants to implement just a few very small things that don't bother me, because I'm unfamiliar with the AJAX framework of. NET beyond these, so it's natural to think of the built-in script callback mechanism that leverages He himself agreed to a friend of the project, we wrote a lot of code on this, after writing I suddenly found a problem, that the code is too messy. Each page is similar and can only pass a string argument so we have to use an inline framework for some large overhead data demonstrations in addition to interactivity. Two days ago to the chart to buy a "Ajax Advanced language Program design," After reading a part of the sudden want to understand ASP.net 2.0 inside the script callback is implemented? In fact, now back to the first look, "Custom Script Callback in asp.net" a lot of places have already said very understand, can be helpless at that time level, a lot of things to see is foggy, patronize to see the effect of the first paste I think the most concise implementation effect, and then to analyze.
Create a new Default.aspx page, add a CheckBox control to the page, and then open the Default.aspx.cs file, _default class to add three inherited interfaces Icallbackcontainer, ICallbackEventHandler and INamingContainer:

[Code 1]

#region Icallbackcontainer Members
public string Getcallbackscript (IButtonControl Buttoncontrol, string argument)
{
throw new Exception ("the method or operation isn't 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 Default.aspx.cs page by adding the following code to the Page_Load method:


[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>


The checkbox control here is casually pulled up, there is a need to change casually, but with the button control may be noted that it will activate the onsubmit event by default, so you may want to set or directly use HTML control. The above code is a script callback to minimize the implementation, are necessary and indispensable.
A control that uses a basic callback must implement three interfaces, respectively: Icallbackcontainer,inamingcontainer and ICallbackEventHandler. In fact, INamingContainer does not have the interface content that needs to be implemented, it simply "identifies a container control that creates a new ID namespace within the control hierarchy of the Page object" (quoted in MSDN). As for the Icallbackcontainer interface, the explanations given in the MSDN (Chinese version) are vague, and some of the associated articles are also about scripting callbacks and ICallbackEventHandler interfaces, because we use the page as the basis for callbacks, So there's no way to use this interface to implement Getcallbackscript, but if you're encapsulating your own Ajax controls it's very useful, and here we're just using the ICallbackEventHandler implementation method to process the data, because in Page_ In the Load method I register a callserver method and then attach to the checkbox's OnClick event to trigger, so that we can view a clearer call procedure.
Later [code 3] I implemented two JavaScript methods, one to handle the call error, and the other to handle the return information after the successful call. In [code 1] There is a piece of code that throws an exception that I have commented out, and this code can be used to simulate invoking the OnError method.
All the way down here we all see the implementation of this call is what, in fact, this is actually a comparison of advanced drag control, but in the end it is how to achieve it? Why don't I see anything about XMLHttpRequest? (I believe this is the best way to implement Ajax, because this code is available on any JavaScript-enabled browser, and I don't think it has anything to do with the secret door.)

Compile, run .....

Clicking that multiple selection box on the running page will show "___i call Server is succeed.___". What exactly is the implementation of this? In fact, just click on the page "View source code" on it, here is a small secret, automatically generated on the page three pieces of script block, one is __dopostback which is used to handle the server control event postback, The other is the Callserer method that we just registered with ClientScript, and the link tag for an external script, which is the key, see the URL of the connection to which the link is:

<script src= "/techtest/webresource.axd?d=de9yrizlddq8oulo_3rqga2&t=632919546726295408" type= "text/ JavaScript "></script>

According to the address above SRC indicated, you can get a WebResource.axd file (add the connection address to the Thunder and so on download tool inside can download to), open can see this file inside actually is some JavaScript code:

Try
{
XMLRequest = new XMLHttpRequest ();
}
catch (E)
{
Try
{
XMLRequest = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (E)
{
}
}

Good familiar code Ah!

if (!useasync)
{
if (__synchronouscallbackindex!=-1)
{
__pendingcallbacks[__synchronouscallbackindex] = null;
}
__synchronouscallbackindex = Callbackindex;
}

Inside this? Isn't that how you handle the asynchronous invocation options in the Page.ClientScript.GetCallbackEventReference method?

Inside are a lot of useful code, I study in .......... ......

In addition to these there are many methods, the white is a very simple AJAX framework encapsulation, there are processing control events with the processing of postback requests, we can do some other encapsulation on this basis, so that can be directly encapsulated into a simple. NET AJAX control, as a pair in the development. NET is a lightweight solution.

is actually Java,. NET, or PHP, is the 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 learned. NET, we can build our own AJAX development environment from some restrictions and rewrite some pages or controls ourselves.

I turned out to be a control worker, I think the package is a big factory era must be a technology, but programmers still have to get to the bottom of the story, the real understanding of the program behind the running of the content so as to better develop a high-quality program.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.