C# 添加用户自定义控件的两种方式

添加用户自定义控件代码
 1 using System;
 2 using System.Data;
 3 using System.Configuration;
 4 using System.Collections;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 
12 namespace UserControls.User
13 {
     
14     public partial class WebUserControl2 : System.Web.UI.UserControl
15     {
16         protected void Page_Load(object sender, EventArgs e)
17         {
18 
19         }
20         public enum ProverbsForMe : int
21         {
22             FourWords = 1,
23             FiveWords = 2,
24             SixWords = 3
25         }
26 
27         public enum ProverbsForOthers : int
28         {
29             FourWords = 1,
30             FiveWords = 2,
31             SixWords = 3
32         }
33         private ProverbsForMe prbMessageForMe;
34         public ProverbsForMe ForMe
35         {
36             get { return prbMessageForMe; }
37             set { prbMessageForMe = value; }
38         }
39 
40         private ProverbsForOthers prbMessageForOthers;
41         public ProverbsForOthers ForOthers
42         {
43             get { return prbMessageForOthers; }
44             set { prbMessageForOthers = value; }
45         }
46 
47         private string strMessageForMe;
48         private string strMessageForOthers;
49 
50         protected override void Render(HtmlTextWriter Output)
51         {
52             switch ((int)prbMessageForMe)
53             {
54                 case 1:
55                     strMessageForMe = "水滴石穿"break;
56                 case 2:
57                     strMessageForMe = "温良恭俭让"break;
58                 case 3:
59                     strMessageForMe = "律己严待人宽"break;
60             }
61 
62             switch ((int)prbMessageForOthers)
63             {
64                 case 1:
65                     strMessageForOthers = "勤能补拙"break;
66                 case 2:
67                     strMessageForOthers = "凡事起头难"break;
68                 case 3:
69                     strMessageForOthers = "今日事今日毕"break;
70             }
71 
72             Output.Write("我的座右铭:" + "<input type='button' value=" + strMessageForMe +
73                "><br>" + "给您的座右铭:" + "<input type='button' value=" +
74                strMessageForOthers + ">");
75         }
76     }
77 }

 

以类方式添加自定义控件代码
 1 using System;
 2 using System.Data;
 3 using System.Configuration;
 4 using System.Web;
 5 using System.Web.Security;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 using System.Web.UI.WebControls.WebParts;
 9 using System.Web.UI.HtmlControls;
10 
11 namespace MyControls
12 {
13     /// <summary>
14     /// ProverbE 自定义控件 枚举类型的属性 层叠式
15     /// </summary>
16     public class ProverbE : Control
17     {
18         public enum ProverbsForMe : int
19         {
20             FourWords = 1,
21             FiveWords = 2,
22             SixWords = 3
23         }
24 
25         public enum ProverbsForOthers : int
26         {
27             FourWords = 1,
28             FiveWords = 2,
29             SixWords = 3
30         }
31 
32         private ProverbsForMe prbMessageForMe;
33         public ProverbsForMe ForMe
34         {
35             get { return prbMessageForMe; }
36             set { prbMessageForMe = value; }
37         }
38 
39         private ProverbsForOthers prbMessageForOthers;
40         public ProverbsForOthers ForOthers
41         {
42             get { return prbMessageForOthers; }
43             set { prbMessageForOthers = value; }
44         }
45         private string strMessageForMe;
46         private string strMessageForOthers;
47 
48         protected override void Render(HtmlTextWriter Output)
49         {
50             switch ((int)prbMessageForMe)
51             {
52                 case 1:
53                     strMessageForMe = "水滴石穿"break;
54                 case 2:
55                     strMessageForMe = "温良恭俭让"break;
56                 case 3:
57                     strMessageForMe = "律己严待人宽"break;
58             }
59 
60             switch ((int)prbMessageForOthers)
61             {
62                 case 1:
63                     strMessageForOthers = "勤能补拙"break;
64                 case 2:
65                     strMessageForOthers = "凡事起头难"break;
66                 case 3:
67                     strMessageForOthers = "今日事今日毕"break;
68             }
69 
70             Output.Write("我的座右铭:" + "<input type='button' value=" + strMessageForMe +
71                "><br>" + "给您的座右铭:" + "<input type='button' value=" +
72                strMessageForOthers + ">");
73         }
74     }
75 }
76 

 

posted @ 2010-09-08 09:08  ®Geovin Du Dream Park™  阅读(859)  评论(0编辑  收藏  举报