Get the App
SLTechnology News&Howtos  ›  Development  › 

ASP.NET AJAX analysis with examples

Shulou Source: shulou.com Published: 2022-06-02 07:51:44 09月15日 Update

This article mainly explains "ASP.NET AJAX example Analysis". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "ASP.NET AJAX example Analysis"!

First take a look at the aspx page:

Default.aspx:

DOCTYPEhtmlPUBLIC "- / / W3C//DTDXHTML1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >

< htmlxmlns= "http://www.w3.org/1999/xhtml" >

< headrunat= "server" >

< title > UntitledPagetitle >

Head >

< body >

< formid= "form1" runat= "server" >

< asp:ScriptManagerID= "ScriptManager1" runat= "server" >

< Scripts >

< asp:ScriptReferenceAssembly= "Microsoft.Web.Preview" Name= "PreviewScript.js" / >

< asp:ScriptReferencePath= "~ / ajax.js" / >

Scripts >

< Services >

< asp:ServiceReferencePath= "~ / SayHelloService.asmx" / >

Services >

Asp:ScriptManager >

< div >

< inputid= "btnSayHello" type= "button" value= "SayHello" / >

< divid= "result" > div >

Div >

Form > 24body > 25html >

There are two changes. One is the addition of a reference to PreviewScript.js in the ScriptManager control. Note here that the content of the client component is not included in the ASP.NET AJAX1.0 formal version, but is replaced in the ASP.NET AJAX Futures CTP section. So, to use these features, first add a reference to the Microsoft.Web.Preview.dll file, which is in the ASP.NET AJAX Futures CTP installation directory, and then add a reference to the PreviewScript.js file on the page.

The second change is that the btnSayHello no longer has the onclick attribute, so how do you know what code needs to be executed when you click this button? The answer is in the js file.

Ajax.js:

Var btnSayHello; var lblResult; Sys.Application.add_init (onPageInit); function onPageInit () {btnSayHello=new Sys.Preview.UI.Button ($get ("btnSayHello")); btnSayHello.initialize (); lblResult=new Sys.Preview.UI.Label ($get ("result")); lblResult.initialize (); btnSayHello.add_click (btnSayHello_onClick);} function btnSayHello_onClick () {SayHelloService.SayHello (OnSucceeded,OnFailded) } function OnSucceeded (resultText) {lblResult.set_text (resultText);} function OnFailded (error) {lblResult.set_text ("call failed. Error message: "+ error.get_message ();}

We can see that great changes have taken place in JavaScript after the application of component programming ideas. Don't worry, we'll parse the file step by step.

At the top is the definition of two global variables that will store references to btnSayHello and result, respectively. The reason why it is defined as a global variable is to facilitate the invocation of two controls throughout the file.

The function of Sys.Application.add_init (onPageInit); is to tell the page to execute a function named onPageInit when the page is initialized.

OnPageInit is a custom function, which mainly completes the creation and initialization of client controls. Take the following two lines of code as an example:

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

* I think most people will know what it means at a glance, because that "new" is so kind, which is to create an instance of Button and assign it to the btnSayHello variable. Where Sys.Preview.UI.Button is the fully qualified name of Button, and most control constructors require a parameter to indicate the DOM element to which the control is associated. The second line is required. After instantiating a control, * * immediately calls the initialize method to avoid some strange situations.

BtnSayHello.add_click (btnSayHello_onClick)

The above line of code associates the click event of the control with the function btnSayHello_onClick. Here I will specifically talk about how to set the properties and events of the ASP.NET AJAX client control.

The ASP.NET AJAX framework stipulates that the control name .get _ property name () method should be used when getting the properties of a control, while the control name .set _ property name () should be set. When you add a listener to an event of a control, you should use "control name. add _ event name ()" and remove it with "control name. remove _ event name ()". This is a naming convention enforced by ASP.NET AJAX, and all client controls follow this rule, and we should follow this rule when developing our own client components.

At this point, I believe that you have a deeper understanding of "ASP.NET AJAX example Analysis", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Controls files events functions clients clients properties variables methods pages analysis two code content components instances rules associations changes learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft OPPO Reno Shulou Tech Info vpn