Asp. NET Script Callback principle

Source: Internet
Author: User

After watching the two episode of Stargate and having 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 成员
public string GetCallbackScript(IButtonControl buttonControl, string argument)
{
   throw new Exception("The method or operation is not implemented.");
}
#endregion
#region ICallbackEventHandler 成员
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>

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.