ASP.NET可折叠自定义控件

效果

2010042116150531.gif

 

CollaspableControl.cs

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 using System;
2   using System.Collections.Generic;
3   using System.ComponentModel;
4   using System.Text;
5   using System.Web;
6   using System.Web.UI;
7   using System.Web.UI.WebControls;
8 using System.Drawing;
9 using System.Web.UI.Design;
10 using System.Web.UI.Design.WebControls;
11
12 namespace CollapsableControl
13 {
14 [ParseChildren( false )]
15 [ToolboxData( " <{0}:CollapsableControl runat=server></{0}:CollapsableControl> " )]
16 [Designer( typeof (CollapsableControlDesigner))]
17 public class CollapsableControl : WebControl
18 {
19 public CollapsableControl()
20 {
21 BorderColor = Color.Transparent;
22 BorderStyle = BorderStyle.Solid;
23 BorderWidth = Unit.Pixel( 1 );
24 TitleColor = Color.FromArgb( 0x66 , 0x99 , 0xcc );
25 TitleHeight = Unit.Pixel( 25 );
26 Height = Unit.Pixel( 120 );
27 Width = Unit.Pixel( 240 );
28 }
29 public string Title { get ; set ; }
30 [TypeConverter( typeof (WebColorConverter))]
31 public Color TitleColor { get ; set ; }
32 [DefaultValue( typeof (Unit), "" )]
33 public Unit TitleHeight { get ; set ; }
34 protected override HtmlTextWriterTag TagKey
35 {
36 get
37 {
38 return HtmlTextWriterTag.Div;
39 }
40 }
41 protected override void OnLoad(EventArgs e)
42 {
43 string url = Page.ClientScript.GetWebResourceUrl( this .GetType(), " CollapsableControl.collapsable.js " );
44 Page.ClientScript.RegisterClientScriptInclude( " CollapsableJS " , url);
45 string arrowUpUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " CollapsableControl.arrow-up.gif " );
46 string arrowDownUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " CollapsableControl.arrow-down.gif " );
47 Page.ClientScript.RegisterClientScriptBlock( this .GetType(), " arrowUpUrl " , string .Format( " var arrowUpUrl=\ " { 0 }\ " ; " , arrowUpUrl), true );
48 Page.ClientScript.RegisterClientScriptBlock( this .GetType(), " arrowDownUrl " , string .Format( " var arrowDownUrl=\ " { 0 }\ " ; " , arrowDownUrl), true );
49 base .OnLoad(e);
50 }
51 protected override void RenderContents(HtmlTextWriter output)
52 {
53 output.AddStyleAttribute(HtmlTextWriterStyle.Width, Width.ToString());
54 output.RenderBeginTag(HtmlTextWriterTag.Div);
55
56 string bgColor = " # " + Convert.ToString(TitleColor.ToArgb(), 16 ).Substring( 2 , 6 );
57 output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, bgColor);
58 output.AddStyleAttribute(HtmlTextWriterStyle.MarginLeft, " 2px " );
59 output.AddStyleAttribute(HtmlTextWriterStyle.MarginRight, " 2px " );
60 output.AddStyleAttribute(HtmlTextWriterStyle.Height, " 1px " );
61 output.RenderBeginTag(HtmlTextWriterTag.Div);
62 output.RenderEndTag();
63
64 output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, bgColor);
65 output.AddStyleAttribute(HtmlTextWriterStyle.MarginLeft, " 1px " );
66 output.AddStyleAttribute(HtmlTextWriterStyle.MarginRight, " 1px " );
67 output.AddStyleAttribute(HtmlTextWriterStyle.Height, " 1px " );
68 output.RenderBeginTag(HtmlTextWriterTag.Div);
69 output.RenderEndTag();
70
71 output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, bgColor);
72 output.AddStyleAttribute(HtmlTextWriterStyle.VerticalAlign, " middle " );
73 if (TitleHeight.Value > 2 )
74 output.AddStyleAttribute(HtmlTextWriterStyle.Height, (TitleHeight.Value - 2 ).ToString() + " px " );
75 output.RenderBeginTag(HtmlTextWriterTag.Div);
76 if ( string .IsNullOrEmpty(Title))
77 output.Write( string .Format( " [{0}] " , this .ID));
78 else
79 output.Write(Title);
80 string arrowUpUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " CollapsableControl.arrow-up.gif " );
81 output.Write( " <img style=\ " float :right;cursor:pointer;\ " src=\ " { 0 }\ " οnclick=\ " collapsableImageClicked( this );\ " /> " , arrowUpUrl);
82 output.RenderEndTag();
83
84 output.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, BorderStyle.Solid.ToString());
85 output.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, BorderWidth.ToString());
86 output.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, bgColor);
87 if ( this .Height.Value > this .TitleHeight.Value)
88 output.AddStyleAttribute(HtmlTextWriterStyle.Height, (Height.Value - TitleHeight.Value).ToString() + " px " );
89 if (DesignMode)
90 output.AddAttribute(HtmlTextWriterAttribute.DesignerRegion, " 0 " );
91 output.RenderBeginTag(HtmlTextWriterTag.Div);
92 base .RenderContents(output);
93 output.RenderEndTag();
94 output.RenderEndTag();
95 }
96 }
97 }

 

CollapsableControlDesigner.cs

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 namespace CollapsableControl
2 {
3 public class CollapsableControlDesigner : ContainerControlDesigner
4 {
5 public override string GetDesignTimeHtml(DesignerRegionCollection regions)
6 {
7 base .GetDesignTimeHtml(regions);
8 WebControl viewControl = base .ViewControl as WebControl;
9 StringBuilder sb = new StringBuilder();
10 using (StringWriter sw = new StringWriter(sb))
11 {
12 using (HtmlTextWriter writer = new HtmlTextWriter(sw))
13 {
14 viewControl.RenderControl(writer);
15 }
16 }
17 return sb.ToString();
18 }
19 }
20 }

 

collapsible.js

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 function collapsableImageClicked(img) {
2 if (img.src.indexOf(arrowUpUrl) > 0 ) {
3 img.src = arrowDownUrl;
4 img.parentNode.nextSibling.style.display = " none " ;
5 }
6 else {
7 img.src = arrowUpUrl;
8 img.parentNode.nextSibling.style.display = " block " ;
9 }
10 }

AssemblyInfo.cs添加WebResource

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 [assembly: WebResource( " CollapsableControl.arrow-up.gif " , " image/gif " )]
2 [assembly: WebResource( " CollapsableControl.arrow-down.gif " , " image/gif " )]
3 [assembly: WebResource( " CollapsableControl.collapsable.js " , " application/x-javascript " , PerformSubstitution = true )]

 

 

转载于:https://www.cnblogs.com/haojuandon/archive/2010/04/21/1717330.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值