Asp.Net WebControl开发
这两天有时间研究了下Web自定义控件的开发,觉得还是满有意思的,跟大家分享一下我的成果!
1.ImageLinkButtom
源代码展示:
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Linq;
5using System.Text;
6using System.Web;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9
10namespace JackSoftControls.UI.Buttons
11{
12 [DefaultProperty("ControlWidth")]
13 [DefaultEvent("OnClick")]
14 [ToolboxData("<{0}:ImageLinkButton runat=server></{0}:ImageLinkButton>")]
15 [System.Drawing.ToolboxBitmap(typeof(ImageLinkButton), "UI.Buttons.ImageLinkButton.bmp")]
16 public class ImageLinkButton : WebControl, INamingContainer, IPostBackEventHandler
17 {
18 private static readonly object _Click;
19 private string PoseBackString = string.Empty;
20 [Description("当图片连接按钮按下时,您希望程序做的事情,请提供功能的详细代码实现")]
21 public event EventHandler OnClick
22 {
23 add
24 {
25 Events.AddHandler(this, value);
26 }
27 remove
28 {
29 Events.RemoveHandler(this, value);
30 }
31 }
32 [Description("指定空间的宽度,一般为图片宽度")]
33 public int ControlWidth
34 {
35 get;
36 set;
37 }
38 [Description("指定空间的高度,一般为图片高度")]
39 public int ControlHeight
40 {
41 get;
42 set;
43 }
44 [Description("指定图片的地址")]
45 [UrlProperty]
46 public string ImageUrl
47 {
48 get;
49 set;
50 }
51 protected override void Render(HtmlTextWriter output)
52 {
53 if ((base.Site != null) && base.Site.DesignMode)
54 {
55 PoseBackString = "JavaScript:__doPostBack('" + this.ClientID.ToString().Trim() + "','1')";
56 output.Write(string.Format("<a href=\"{0}\"><img src=\"{1}\" style=\"width:{2};height:{3};border:0\" /></a>", PoseBackString, this.ImageUrl, this.ControlWidth, this.ControlHeight));
57 }
58 else
59 {
60 base.Render(output);
61 }
62 }
63 protected override void CreateChildControls()
64 {
65 PoseBackString = "JavaScript:__doPostBack('" + this.ClientID.ToString().Trim() + "','1')";
66 string ImageButtonContext = string.Format("<a href=\"{0}\"><img src=\"{1}\" style=\"width:{2};height:{3};border:0\" /></a>", PoseBackString, this.ImageUrl, this.ControlWidth, this.ControlHeight);
67 LiteralControl Lin1 = new LiteralControl(ImageButtonContext);
68 Controls.Add(Lin1);
69 }
70
71 IPostBackEventHandler 成员#region IPostBackEventHandler 成员
72 public void RaisePostBackEvent(string eventArgument)
73 {
74 TextOnClick(this, EventArgs.Empty);
75 }
76 #endregion
77
78 protected void TextOnClick(object sender, EventArgs e)
79 {
80 EventHandler hd = (EventHandler)base.Events[this];
81 if (hd != null)
82 {
83 hd(sender, e);
84 }
85 }
86 }
87}
88
附Demo以供测试!
谢谢大家这么长时间的支持!
下载该Demo 版本号:1.0.9719.2 证书密码:840525