ASP.NET AJAX 好用的javascript function ' $get '
在asp.net ajax 裡很多不錯用的function , 例如 $get 或 $find
用一個實例介紹$get的用法給大家知道呀...
c#範例
.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02
03 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>Untitled Page</title>
08 </head>
09 <body>
10 <form id="form1" runat="server">
11 <asp:ScriptManager ID="ScriptManager1" runat="server" />
12
13 <script language="javascript" type="text/javascript">
14 function ChangeColor(color)
15 {
16 //ASP.NET AJAX 取控制項的方法(此方法必須在有ScriptManager的控制項才能使用)
17 var div = $get('div1');
18
19 //一般取控制項的方法
20 //var div = document.getElementById('div1');
21
22 div.style.color=color;
23
24 }
25 </script>
26
27 <div id="div1">
28 test text color...
29 </div>
30 <asp:Button ID="red" runat="server" Text="red" OnClientClick="ChangeColor('red'); return false;" />
31 <asp:Button ID="red0" runat="server" Text="greend" OnClientClick="ChangeColor('green'); return false;" />
32 <asp:Button ID="red1" runat="server" Text="blue" OnClientClick="ChangeColor('blue'); return false;" />
33 </form>
34 </body>
35 </html>
36
02
03 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>Untitled Page</title>
08 </head>
09 <body>
10 <form id="form1" runat="server">
11 <asp:ScriptManager ID="ScriptManager1" runat="server" />
12
13 <script language="javascript" type="text/javascript">
14 function ChangeColor(color)
15 {
16 //ASP.NET AJAX 取控制項的方法(此方法必須在有ScriptManager的控制項才能使用)
17 var div = $get('div1');
18
19 //一般取控制項的方法
20 //var div = document.getElementById('div1');
21
22 div.style.color=color;
23
24 }
25 </script>
26
27 <div id="div1">
28 test text color...
29 </div>
30 <asp:Button ID="red" runat="server" Text="red" OnClientClick="ChangeColor('red'); return false;" />
31 <asp:Button ID="red0" runat="server" Text="greend" OnClientClick="ChangeColor('green'); return false;" />
32 <asp:Button ID="red1" runat="server" Text="blue" OnClientClick="ChangeColor('blue'); return false;" />
33 </form>
34 </body>
35 </html>
36
結果: