asp.net ajax client Programming trip (iv)

Source: Internet
Author: User

asp.net AJAX Client Programming Tour (IV)--Developing AJAX applications with the idea of components: A preliminary study of client components

Re-examining AJAX client development with component ideas

Before going into the subject, I would like to take a look at one of the demo:aspnetajaxtest in the first article in this series. I will now post the client code here again.

Default.aspx:

1<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
2
3<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
456 <title>untitled page</title>
78<body>
9 <form id= "Form1" runat= "Server" >
<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
<Scripts>
<asp:scriptreference path= "~/ajax.js"/>
</Scripts>
<Services>
<asp:servicereference path= "~/sayhelloservice.asmx"/>
</Services>
</asp:ScriptManager>
<div>
<input id= "Btnsayhello" type= "button" value= "SayHello" onclick= "Btnsayhello_onclick ()"/>
<div id= "Result" ></div>
</div>
</form>
23</body>
24

Ajax.js:

1//单击btnSayHello时调用的JS函数
2function btnSayHello_onClick()
3{
4  SayHelloService.SayHello(OnSucceeded,OnFailded);
5}
6
7//成功时的回调函数
8function OnSucceeded(reusltText)
9{
10  $get("result").innerHTML=reusltText;
11}
12
13//失败时的回掉函数
14function OnFailded(error)
15{
16  $get("result").innerHTML="调用失败。错误信息:"+error.get_message();
17}

This is a familiar JavaScript programming pattern: Based on DOM operations. In this programming mode, all page elements are considered to be a tree-like collection of DOM elements, using the appropriate DOM operation whether to get data or to change the attributes of a page element. For example, to get the value in a text box, first use the document.getElementById (abbreviated as $get in the ASP.net ajax framework) to get the DOM reference for the text box, and then get the value of its Value property.

This programming pattern can easily confuse us, such as the text box, check box, submit button, Normal button DOM representation is "input" element, in addition to use such as innerHTML is not very intuitive name set div or span values. In fact, for developers, especially those who have been in server-side development for a long time, we prefer to think of text boxes as a textbox, a CheckBox, a button, a div or span as a label, and, Instead of using innerHTML, we would prefer to use statements such as Label1.Text to set the text in the Div.

Luckily, ASP.net Ajax developers are obviously taking this into account, and now the ASP.net AJAX framework allows us to use a component-like programming model such as the server side to program clients.

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.