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.