效果
CollaspableControl.cs
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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 }
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](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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 }
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](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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 }
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](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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 )]
2 [assembly: WebResource( " CollapsableControl.arrow-down.gif " , " image/gif " )]
3 [assembly: WebResource( " CollapsableControl.collapsable.js " , " application/x-javascript " , PerformSubstitution = true )]