很多時候,我們需要用到User Control,將部份UI或商務邏輯封裝,下面將UserControl封裝成Asp.Net ajax 控制項:
簡單樣本:
(ASCX) 這一段代碼就不解釋了:
1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LoginPanel.ascx.cs" Inherits="LoginPanel" %>
2 <table>
3 <tr>
4 <td>
5 Login Name:
6 </td>
7 <td>
8 <asp:TextBox ID="UserName" Ruant="Server"></asp:TextBox>
9 </td>
10 </tr>
11 <tr>
12 <td>
13 Password:
14 </td>
15 <td>
16 <asp:TextBox ID="Password" TextMode="Password" Ruant="Server"></asp:TextBox>
17 </td>
18 </tr>
19 </table>
(LoginPanel.js)
1 <script type="text/javascript">
2 /// <reference name="MicrosoftAjax.js"/>
3 Type.registerNamespace("CsharpFarmer");
4 CsharpFarmer.LoginPanel= function(element) {
5 CsharpFarmer.LoginPanel.initializeBase(this, [element]);
6 this.userName = null;
7 this.password = null;
8 }
9 CsharpFarmer.LoginPanel.prototype = {
10 initialize: function() {
11 CsharpFarmer.LoginPanel.callBaseMethod(this, 'initialize');
12 // Add custom initialization here
13 },
14 get_userName: function() {
15 return this.userName ;
16 },
17 set_userName: function(value) {
18 this.userName = value;
19 },
20 get_password: function() {
21 return this.password ;
22 },
23 set_password: function(value) {
24 this.password = value;
25 },
26 dispose: function() {
27 //Add custom dispose actions here
28 CsharpFarmer.LoginPanel.callBaseMethod(this, 'dispose');
29 delete this.userName;
30 delete this.password;
31 }
32 }
33 CsharpFarmer.LoginPanel.registerClass(CsharpFarmer.LoginPanel, Sys.UI.Control);
34 </script>