ASP. NET AJAX

Source: Internet
Author: User

Let's take a look at the application re-compiled using the component idea. First, let's take a look at the aspx page:

Default. aspx:

 
 
  1. DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">  
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">  
  3. <headrunat="server">  
  4. <title>UntitledPagetitle>  
  5. head>  
  6. <body>  
  7. <formid="form1"runat="server">  
  8. <asp:ScriptManagerID="ScriptManager1"runat="server">  
  9. <Scripts>  
  10. <asp:ScriptReferenceAssembly="Microsoft.Web.Preview"Name="PreviewScript.js"/>  
  11. <asp:ScriptReferencePath="~/ajax.js"/>  
  12. Scripts>  
  13. <Services>  
  14. <asp:ServiceReferencePath="~/SayHelloService.asmx"/>  
  15. Services>  
  16. asp:ScriptManager>  
  17. <div>  
  18. <inputid="btnSayHello"type="button"value="SayHello"/>  
  19. <divid="result">div>  
  20. div>  
  21. form>24body>25html> 

There are two changes here. One is to add a reference to PreviewScript. js in the ScriptManager control. Note that the content of the client component is not included in the official version of ASP. NET AJAX1.0, but included in ASP. net ajax Futures CTP. Therefore, to use these features, you must first add. web. preview. dll file reference. This file is stored in ASP. net ajax Futures CTP installation directory, and then add PreviewScript to the page. js file reference.

The second change is that btnSayHello has no onclick attribute. How do I know the code to be executed when I click this button? The answer is in the js file.

Ajax. js:

 
 
  1. Var btnSayHello;
  2. Var lblResult;
  3. Sys. Application. add_init (onPageInit );
  4. Function onPageInit ()
  5. {BtnSayHello=NewSys. Preview. UI. Button ($ get ("btnSayHello "));
  6. BtnSayHello. initialize ();
  7. LblResult=NewSys. Preview. UI. Label ($ get ("result "));
  8. LblResult. initialize (); btnSayHello. add_click (btnSayHello_onClick );
  9. }
  10. Function btnSayHello_onClick (){
  11. SayHelloService. SayHello (OnSucceeded, OnFailded );
  12. } Function OnSucceeded (resultText ){
  13. LblResult. set_text (resultText );
  14. } Function OnFailded (error ){
  15. LblResult. set_text ("Call failed. Error message: "+ error. get_message ());}


We can see that JavaScript has undergone a huge change after applying the component programming idea. Don't worry. Let's parse this file step by step.

Two global variables are defined at the top, which store references to btnSayHello and result respectively. To define global variables, you can easily call two controls in the entire file.

Sys. Application. add_init (onPageInit); is used to tell the page to execute the onPageInit function when the page is initialized.

OnPageInit is a custom function that mainly creates and initializes client controls. The following two lines of code are used as an example:

 
 
  1. btnSayHello=new Sys.Preview.UI.Button($get("btnSayHello"));  
  2. btnSayHello.initialize(); 


In the first line, I think most people will know what it means, because the "new" is very friendly. This is the creation of a Button instance and assigning it to the btnSayHello variable. Among them, Sys. Preview. UI. Button is the fully qualified name of the Button, and most control constructors need a parameter to indicate the DOM element to be associated with the control. The second line is required. After instantiating a control, you 'd better call the initialize method immediately to avoid some strange situations.

 
 
  1. btnSayHello.add_click(btnSayHello_onClick); 


The above code associates the Click Event of the control with the function btnSayHello_onClick. Here I want to talk about the properties of ASP. NET AJAX client controls and how to set events.

ASP. the net ajax framework specifies that the "control name. get _ property name () ", which is set to" control name. set _ attribute name ()". When adding a listening function for an event of a control, you should use "Control name. add _ event name ()" and "Control name. remove _ event name ()" When removing the function ()". This is the naming convention enforced by ASP. net ajax. All client controls follow this rule. In the future, we should also follow this rule when developing our own client components.

With the above knowledge, a lot of code is easy to understand. For example, "lblResult. set_text (resultText);" sets the text attribute of the lblResult control to resultText. I will not explain the remaining code. It should be okay.

  1. Introduction to ASP. net mvc Framework
  2. Introduction to MvcAjaxPanel in ASP. NET MVC
  3. ASP. net mvc Framework to save UpdatePanel
  4. Use ASP. net mvc source code to find a solution
  5. ActionInvoker of ASP. net mvc Framework

Related Article

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.