可以申縮的自定義Panel收藏

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Security.Policy;
using System.IO;
using System.Text;

namespace Asm.STJ.MIS.WebControls
{
 /// <summary>
 /// CollapablePanel 的摘要描述。
 /// </summary>
 [DefaultProperty("Item"),ParseChildren(true,"Items"),
 ToolboxData("<{0}:CollapablePanel runat=server></{0}:CollapablePanel>")]
 public class CollapablePanel : System.Web.UI.WebControls.WebControl,IPostBackEventHandler,INamingContainer
 {
  #region Event definition

  public event ItemClickEventHandler ItemClick;

  #endregion

  #region private variable definition.

  private string _titleText;
  private string _titleHeadImg;
  private string _titleEndImgOpen;
  private string _titleEndImgClose;
  private Unit _titleHeight;
  private string _titleBackImg;
  
  private Unit _width;
  private Unit _prefix;
  private string _itemBackImg;
  private Unit _itemHeight;
  private PanelItems _items;

  private bool _opened;
  private Style _titleStyle;
  private Style _itemStyle;

  #endregion
 
  #region attribute definition.

  [
  Bindable(true),
  Category("Title"),
  DefaultValue("Title Text"),
  Description("Set or get the text of title.")
  ]
  public string TitleText
  {
   get
   {
    return (this._titleText == null)? "Title Text" : this._titleText;
   }

   set
   {
    this._titleText = value;
   }
  }

  [
  Bindable(true),
  Category("Title"),
  Description("Set or get the head image of title.")
  ]
  public string TitleHeadImage
  {
   get
   {
    if ( this._titleHeadImg == null )
    {
     this._titleHeadImg = String.Empty;
    }
    return this._titleHeadImg;
   }

   set
   {
    this._titleHeadImg = value;
   }
  }

  [
  Bindable(true),
  Category("Title"),
  Description("Set or get the end image of title.")
  ]
  public string EndImageOpen
  {
   get
   {
    if ( this._titleEndImgOpen == null )
    {
     this._titleEndImgOpen = String.Empty;
    }
    return this._titleEndImgOpen;
   }

   set
   {
    this._titleEndImgOpen = value;
   }
  }

  [
  Bindable(true),
  Category("Title"),
  Description("Set or get the end image of title.")
  ]
  public string EndImageClose
  {
   get
   {
    if ( this._titleEndImgClose == null )
    {
     this._titleEndImgClose = String.Empty;
    }
    return this._titleEndImgClose;
   }

   set
   {
    this._titleEndImgClose = value;
   }
  }

  [
  Bindable(true),
  Category("Title"),
  Description("Set or get the end image of title.")
  ]
  public string TitleBackImage
  {
   get
   {
    if ( this._titleBackImg == null )
    {
     this._titleBackImg = String.Empty;
    }
    return this._titleBackImg;
   }

   set
   {
    this._titleBackImg = value;
   }
  }

  [
  Bindable(true),
  Category("Item"),
  Description("Set or get the end background image of title.")
  ]
  public string ItemBackImage
  {
   get
   {
    if ( this._itemBackImg == null )
    {
     this._itemBackImg = String.Empty;
    }
    return this._itemBackImg;
   }

   set
   {
    this._itemBackImg = value;
   }
  }

  [
  Bindable(true),
  Category("Item"),
  Description("Set or get the end image of title.")
  ]
  public   Unit PrefixWidth
  {
   get
   {
    return (this._prefix == Unit.Empty)? Unit.Pixel(25) : this._prefix;
   }

   set
   {
    this._prefix = value;
   }
  }

  [
  Bindable(true),
  Category("Title"),
  Description("Set or get the height of title.")
  ]
  public   Unit TitleHeight
  {
   get
   {
    return this._titleHeight;
   }

   set
   {
    this._titleHeight = value;
   }
  }

  [
  Bindable(true),
  Category("Item"),
  Description("Set or get the height of Items.")
  ]
  public   Unit ItemHeight
  {
   get
   {
    return this._itemHeight;
   }

   set
   {
    this._itemHeight = value;
   }
  }

  [
  Bindable(true),
  Category("Appearance")
  ]
  public bool Opened
  {
   get
   {
    return this._opened;
   }

   set
   {
    this._opened = value;
   }
  }

  [
  Bindable(true),
  Category("Layout")
  ]
  public override  Unit Width
  {
   get
   {
    return this._width;
   }

   set
   {
    this._width = value;
   }
  }

  [
  Bindable(true),
  Category("Layout")
  ]
  public override  Unit Height
  {
   get
   {
    return base.Height;
   }
  }

  [
  Category("Item"),
  Description("Item List"),
  DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  NotifyParentProperty(true),
  PersistenceMode(PersistenceMode.InnerDefaultProperty)
  ]
  public PanelItems Items
  {
   get
   {
    if ( this._items == null )
    {
     this._items = new  PanelItems();
    }
    return this._items;
   }
  }

  [
  Category("Title"),
  Description("Title Style"),
  DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  NotifyParentProperty(true)
  ]
  public virtual Style TitleStyle
  {
   get
   { 
    if ( this._titleStyle == null )
    {
     this._titleStyle = new Style();
     if ( this.IsTrackingViewState )
     {
      ((IStateManager)this._titleStyle).TrackViewState();
     }
    }
    return this._titleStyle;
   }
  }

  [
  Category("Item"),
  Description("Items Style"),
  DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  NotifyParentProperty(true)
  ]
  public virtual Style ItemStyle
  {
   get
   {
    if ( this._itemStyle == null )
    {
     this._itemStyle = new Style();
     if ( this.IsTrackingViewState )
     {
      ((IStateManager)this._itemStyle).TrackViewState();
     }
    }

    return this._itemStyle;
   }
  }

  #endregion

  #region render filed
  protected override void AddAttributesToRender(HtmlTextWriter writer)
  {
   writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Border,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Width,this._width.Value.ToString());
   
   base.AddAttributesToRender (writer);
  }

  protected override void RenderContents(HtmlTextWriter writer)
  {

   writer.RenderBeginTag(HtmlTextWriterTag.Table); 
   
   if ( this.Page.IsPostBack == true )
   {
    if ( this.Page.Request.Form[this.ClientID+"Opened"] == "True" )
    {
     this.Opened = true;
    }
    else
    {
     this.Opened = false;
    }
   }

   this.RenderTitle(writer);

   this.RenderItems(writer);
   
   writer.RenderEndTag(); 
   this.Page.RegisterHiddenField(this.ClientID+"Opened",this.Opened.ToString());
  }

  private void RenderItems( HtmlTextWriter writer )
  {
   writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Border,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Width,this._width.ToString());
   writer.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID+"child");
   if ( this.Opened == false )
   {
    writer.AddStyleAttribute("display","none");
   }
   writer.RenderBeginTag(HtmlTextWriterTag.Table); 

   //write Items.
   foreach ( PanelItem itm in this.Items )
   {
    this.ItemStyle.AddAttributesToRender(writer);
    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage,"url("+this.ItemBackImage+")");
    if ( this.Enabled )
    {
     writer.AddAttribute("onclick","javascript:{"+this.Page.GetPostBackEventReference(this,itm.Text)+";}");
    }
    writer.AddStyleAttribute("cursor","hand");
    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    //Item Prefix.
    writer.AddAttribute(HtmlTextWriterAttribute.Height,this.ItemHeight.ToString());
    writer.AddAttribute(HtmlTextWriterAttribute.Width,this._prefix.ToString());
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    writer.RenderEndTag();
    
    //Item Image.
    if ( itm.ImageUrl != String.Empty )
    {
     writer.AddAttribute(HtmlTextWriterAttribute.Width,"1");
     writer.RenderBeginTag(HtmlTextWriterTag.Td);
     writer.AddAttribute(HtmlTextWriterAttribute.Src,itm.ImageUrl);    
     writer.RenderBeginTag(HtmlTextWriterTag.Img);
     writer.RenderEndTag();
     writer.RenderEndTag();
    }
    else
    {
     writer.RenderBeginTag(HtmlTextWriterTag.Td);
     writer.RenderEndTag();
    }

    //Item Text.
    writer.AddAttribute(HtmlTextWriterAttribute.Align,"left");
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.Write(itm.Text);
    writer.RenderEndTag();
    writer.RenderEndTag();

    writer.RenderEndTag();
   }

   writer.RenderEndTag();
  }

  private void RenderTitle( HtmlTextWriter writer )
  {
   //write title.

   writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Border,"0");
   writer.AddAttribute(HtmlTextWriterAttribute.Width,this._width.ToString());
   writer.RenderBeginTag(HtmlTextWriterTag.Table); 

   StringBuilder js = new StringBuilder();
   js.Append("javascript:{");
   js.Append("var child = document.getElementById('"+this.ClientID+"child');");
   js.Append(" if (child.style.display == 'none' ) ");
   js.Append("{ child.style.display = 'block';");
   js.Append(" document.getElementById('"+this.ClientID+"Opened').value = 'True';");
   if ( this.EndImageOpen != String.Empty )
   {
    js.Append("var endImg = document.getElementById('"+this.ClientID+"titleEnd');");
    js.Append("if ( endImg  != null ) endImg.src = '"+this.EndImageOpen + "';");
   }
   js.Append("}");

   js.Append(" else { child.style.display = 'none';");
   js.Append(" document.getElementById('"+this.ClientID+"Opened').value = 'False';");
   if ( this.EndImageClose != String.Empty )
   {
    js.Append("var endImg = document.getElementById('"+this.ClientID+"titleEnd');");
    js.Append("if ( endImg  != null ) endImg.src = '"+this.EndImageClose + "';");
   }
   js.Append("}");
   js.Append("}");

   writer.AddAttribute(HtmlTextWriterAttribute.Onclick,js.ToString());
   this.TitleStyle.AddAttributesToRender(writer);
   writer.AddStyleAttribute("cursor","hand");
   writer.AddAttribute(HtmlTextWriterAttribute.Height,this.TitleHeight.ToString());
   writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage,"url('"+this.TitleBackImage+"')");
   writer.RenderBeginTag(HtmlTextWriterTag.Tr);

   if ( this.TitleHeadImage != String.Empty )
   {
    writer.AddAttribute(HtmlTextWriterAttribute.Width,"20");
    writer.AddAttribute(HtmlTextWriterAttribute.Align,"right");
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    writer.AddAttribute(HtmlTextWriterAttribute.Src,this.TitleHeadImage.ToString());
    writer.RenderBeginTag(HtmlTextWriterTag.Img);
    writer.RenderEndTag();
    writer.RenderEndTag();
   }

   writer.RenderBeginTag(HtmlTextWriterTag.Td);
   writer.RenderBeginTag(HtmlTextWriterTag.A);
   writer.Write(this.TitleText);
   writer.RenderEndTag();
   writer.RenderEndTag();

   if ( this.Opened )
   {
    if ( this.EndImageOpen != String.Empty )
    {
     writer.AddAttribute(HtmlTextWriterAttribute.Width,"1");
     writer.AddAttribute(HtmlTextWriterAttribute.Align,"left");
     writer.RenderBeginTag(HtmlTextWriterTag.Td);
     writer.AddAttribute(HtmlTextWriterAttribute.Src,this.EndImageOpen.ToString());
     writer.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID+"titleEnd");
     writer.RenderBeginTag(HtmlTextWriterTag.Img);
     writer.RenderEndTag();
     writer.RenderEndTag();

     writer.AddAttribute(HtmlTextWriterAttribute.Width,"15");
     writer.RenderBeginTag(HtmlTextWriterTag.Td);
     writer.RenderEndTag();
    }
   }
   else
   {
    if ( this.EndImageClose != String.Empty )
    {
     writer.AddAttribute(HtmlTextWriterAttribute.Width,"1");
     writer.AddAttribute(HtmlTextWriterAttribute.Align,"left");
     writer.RenderBeginTag(HtmlTextWriterTag.Td);
     writer.AddAttribute(HtmlTextWriterAttribute.Src,this.EndImageClose.ToString());
     writer.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID+"titleEnd");
     writer.RenderBeginTag(HtmlTextWriterTag.Img);
     writer.RenderEndTag();
     writer.RenderEndTag();

     writer.AddAttribute(HtmlTextWriterAttribute.Width,"15");
     writer.RenderBeginTag(HtmlTextWriterTag.Td);
     writer.RenderEndTag();
    }
   }

   writer.RenderEndTag();
   writer.RenderEndTag();
  }
  #endregion

  #region IPostBackEventHandler 成員

  public void RaisePostBackEvent(string eventArgument)
  {
   ItemClickEventArgs e = new ItemClickEventArgs(eventArgument);
   this.OnItemClick(this,e);
  }

  #endregion

  #region Event Field

  private void OnItemClick(object sender,ItemClickEventArgs e )
  {
   if ( this.ItemClick != null )
   {
    this.ItemClick(sender,e);
   }
  }

  #endregion
 }

 #region Event Args

 public delegate void ItemClickEventHandler(Object sender, ItemClickEventArgs e);

 public class ItemClickEventArgs : EventArgs
 {
  private string _itemText;
  public string ItemText
  {
   get
   {
    return this._itemText;
   }
  }

  public ItemClickEventArgs(string text)
  {
   this._itemText = text;
  }
 }

 #endregion

using System;

namespace Asm.STJ.MIS.WebControls
{
 /// <summary>
 ///
 /// </summary>
 public class ItemConverter : System.ComponentModel.ExpandableObjectConverter
 {
  public ItemConverter()
  {
   //
   // TODO: 在此加入建構函式的程式碼
   //
  }

  public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
  {
   if ( sourceType == typeof(string ) )
   {
    return true;
   }
   return base.CanConvertFrom (context, sourceType);
  }

  public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  {
   if ( value is string )
   {
    PanelItem it = new PanelItem();
    it.Text = value.ToString();
//    it.ImageUrl = "String.Empty";
    return it;
   }
   return base.ConvertFrom (context, culture, value);
  }

  public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
  {
   if ( destinationType == typeof (string ) )
   {
    return ((PanelItem)value).Text;
   }
   
   return base.ConvertTo (context, culture, value, destinationType);
  }

}
}

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Security.Policy;

namespace Asm.STJ.MIS.WebControls
{
 [TypeConverter(typeof(ItemConverter))]
 public class PanelItem
 {
  private string _text;
  private string _imgUrl;
  
  [
  Bindable(true),
  Description("Item Text"),
  NotifyParentProperty(true)
  ]
  public string Text
  {
   get
   {
    return this._text == null? String.Empty:this._text;
   }
   set
   {
    this._text = value;
   }
  }

  [
  Bindable(true),
  Description("Item image Url"),
  NotifyParentProperty(true)
  ]
  public string ImageUrl
  {
   get
   {
    return this._imgUrl == null? "String.Empty":this._imgUrl;
   }
   set
   {
    this._imgUrl = value;
   }
  }
 }

 public class PanelItems : System.Collections.CollectionBase
 {
  private ArrayList _itemArrayList;

  public PanelItems()
  {
   this._itemArrayList = new ArrayList();
  }

  public int Add( PanelItem _item )
  {
   return this.List.Add( _item );
  }

  public void Remove( PanelItem _item )
  {
   this.List.Remove( _item );
  }

  public int Cout
  {
   get
   {
    return this.List.Count;
   }
  }

  public PanelItem this[int index]
  {
   get
   {
    return (PanelItem) this._itemArrayList[index];
   }
   set
   {
    this._itemArrayList[index] = value;
   }
  }
 }
}

 

Lad.li

posted @ 2009-02-22 23:23  silverPerson  阅读(414)  评论(0编辑  收藏  举报