DropDownMenu
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Drawing.Design;
[ assembly:TagPrefix("Damao", "Damao") ]
namespace Damao
{
public class MenuBuilder : ControlBuilder
{
public override Type GetChildControlType( string tagName, IDictionary attribs )
{
if( tagName.ToUpper().EndsWith( "MENUITEM" ) )
return typeof( MenuItem );
return null;
}
public override bool AllowWhitespaceLiterals()
{
return false;
}
public override void AppendLiteralString( string s )
{
}
}
public enum ViewStyle
{
TEXT,
IMAGE
}
public enum AlignStyle
{
Left,Center,Right
}
[
ToolboxData("<{0}:DropdownMenu runat=server></{0}:DropdownMenu>"),
Designer("Damao.MenuDesigner"),
ControlBuilderAttribute( typeof( MenuBuilder ) ),
// ParseChildren(true),
DefaultProperty( "Items" )
]
public class DropdownMenu : System.Web.UI.WebControls.WebControl//,INamingContainer
{
private System.Text.StringBuilder strJSToolbar = new System.Text.StringBuilder();
private ItemCollection _elements;
private Color _subBackColor = Color.Empty;
private Color _subForeColor = Color.Empty;
private Color _subBorderColor = Color.Empty;
private Color _mouseOverColor = Color.SkyBlue;
private Color _submouseOverColor = Color.SkyBlue;
private Unit _subBorderWidth;
private Unit _subBorderCellSpacing;
private Unit _borderCellSpacing;
private Unit _subItemWidth;
private Unit _cellPadding;
private Unit _subCellPadding;
private Unit _subHeight;
// private Style _subStyle;// = new Style();
private BorderStyle _subBorderStyle;
private ViewStyle _viewStyle;
private AlignStyle _align;
private AlignStyle _subAlign;
private string _img;
private string _subImg;
private Panel m_Div = new Panel();
private string _cssPath;
public DropdownMenu()
{
}
[
Browsable( true ),
DefaultValue( null ),
Category("Appearance"),
PersistenceMode( PersistenceMode.InnerProperty)
]
public ItemCollection Items
{
get
{
if( _elements == null )
_elements = new ItemCollection( this );
return _elements;
}
}
[
Category("Appearance"),
Description("控制是否在Item中显示图标。")
]
public ViewStyle ViewStyle
{
set
{
this._viewStyle = value;
}
get
{
return ( ( _viewStyle == ViewStyle.TEXT ) ? ViewStyle.TEXT : ViewStyle.IMAGE );
}
}
[
Category("Appearance"),
Description("子菜单的背景颜色。")
]
public Color SubBackColor
{
get
{
return ( ( _subBackColor == Color.Empty ) ? Color.Silver : _subBackColor );
}
set
{
_subBackColor = value;
}
}
[
Category("Appearance"),
Description("子菜单的前景颜色。")
]
public Color SubForeColor
{
get
{
return ( ( this._subForeColor == Color.Empty ) ? Color.Black : _subForeColor );
}
set
{
_subForeColor = value;
}
}
[
Category("Appearance"),
Description("子菜单的边框颜色。")
]
public Color SubBorderColor
{
get
{
return ( ( this._subBorderColor == Color.Empty ) ? Color.Gray : _subBorderColor );
}
set
{
_subBorderColor = value;
}
}
[
Category("Appearance"),
Description("主菜单鼠标滑过时的颜色。")
]
public Color MainMouseOverColor
{
get
{
return ( ( this._mouseOverColor == Color.Empty ) ? Color.SkyBlue : _mouseOverColor );
}
set
{
_mouseOverColor = value;
}
}
[
Category("Appearance"),
Description("用于子菜单的样式表文件路径。")
]
public string CssFilePath
{
get
{
return this._cssPath;
}
set
{
this._cssPath = value;
}
}
[
Category("Appearance"),
Description("子菜单鼠标滑过时颜色。")
]
public Color SubMouseOverColor
{
get
{
return ( ( this._submouseOverColor == Color.Empty ) ? Color.SkyBlue : _submouseOverColor );
}
set
{
_submouseOverColor = value;
}
}
[
Category("Appearance"),
Description("子菜单BorderWidth。")
]
public Unit SubBorderWidth
{
get
{
return ( ( this._subBorderWidth == Unit.Empty ) ? Unit.Parse("0") : _subBorderWidth );
}
set
{
_subBorderWidth = value;
}
}
[
Category("Appearance"),
Description("子菜单CellSpacing。")
]
public Unit SubCellSpacing
{
get
{
return ( ( this._subBorderCellSpacing == Unit.Empty ) ? Unit.Parse("0") : _subBorderCellSpacing );
}
set
{
_subBorderCellSpacing = value;
}
}
[
Category("Appearance"),
Description("主菜单CellSpacing。")
]
public Unit CellSpacing
{
get
{
return ( ( this._borderCellSpacing == Unit.Empty ) ? Unit.Parse("0") : _borderCellSpacing );
}
set
{
_borderCellSpacing = value;
}
}
[
Category("Appearance"),
Description("子菜单项高度。")
]
public Unit SubItemHeight
{
get
{
return ( ( this._subHeight == Unit.Empty ) ? Unit.Parse("24") : _subHeight );
}
set
{
_subHeight = value;
}
}
[
Category("Appearance"),
Description("主菜单CellPadding。")
]
public Unit CellPadding
{
get
{
return ( ( this._cellPadding == Unit.Empty ) ? Unit.Parse("0") : _cellPadding );
}
set
{
_cellPadding = value;
}
}
[
Category("Appearance"),
Description("子菜单CellPadding。")
]
public Unit SubCellPadding
{
get
{
return ( ( this._subCellPadding == Unit.Empty ) ? Unit.Parse("0") : _subCellPadding );
}
set
{
_subCellPadding = value;
}
}
[
Category("Appearance"),
Description("子菜单项宽度。")
]
public Unit ItemWidth
{
get
{
return ( ( this._subItemWidth == Unit.Empty ) ? Unit.Parse("100") : _subItemWidth );
}
set
{
_subItemWidth = value;
}
}
[
Category("Appearance"),
Description("子菜单BorderStyle。")
]
public BorderStyle SubBorderStyle
{
get
{
return ( ( this._subBorderStyle == BorderStyle.NotSet ) ? BorderStyle.NotSet : _subBorderStyle );
}
set
{
_subBorderStyle = value;
}
}
public override Color BackColor
{
get
{
return ( ( base.BackColor == Color.Empty ) ? Color.WhiteSmoke : base.BackColor );
}
set
{
base.BackColor = value;
}
}
[
Category("Appearance"),
Description("主菜单对齐方式。")
]
public AlignStyle Align
{
get
{
return this._align;
}
set
{
this._align = value;
}
}
[
Category("Appearance"),
Description("子菜单对齐方式。")
]
public AlignStyle SubAlign
{
get
{
return this._subAlign;
}
set
{
this._subAlign = value;
}
}
[Browsable( false )]
public new FontInfo Font
{
get{return null;}
}
// [Browsable( true )]
// public FontInfo SubFont
// {
// get
// {
// if(this._subStyle==null)
// this._subStyle = new Style();
// return _subStyle.Font;
// }
//// set
//// {
//// this._subStyle = value;
//// }
// }
public new Unit Width
{
get
{
return Unit.Parse(Convert.ToString(this.Items.Count*this.ItemWidth.Value));
}
}
public override Unit Height
{
get
{
return ( ( base.Height == Unit.Empty ) ? Unit.Parse("21") : base.Height );
}
set
{
base.Height = value;
}
}
[EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor)),
Description("主菜单背景图。")]
public string BackGround
{
set
{
_img = value;
}
get
{
return _img;
}
}
[EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor)),
Description("子菜单背景图。")]
public string SubBackGround
{
set
{
_subImg = value;
}
get
{
return _subImg;
}
}
[Bindable(false),
Category("Appearance")
]
public string AuthorInfo
{
get
{
return "Author:Damao(Gu Qinggang)|Email:Damao@da-mao.com|2004-6-17";
}
}
protected override object SaveViewState()
{
object _baseState = base.SaveViewState();
object _menuState = ((IStateManager)Items).SaveViewState();
object[] _newState = new object[2];
_newState[0] = _baseState;
_newState[1] = _menuState;
return _newState;
}
protected override void LoadViewState( object state )
{
if( state != null )
{
object[] _newState = (object[])state;
if( _newState[0] != null )
base.LoadViewState( _newState[0] );
if( _newState[1] != null )
((IStateManager)Items).LoadViewState( _newState[1] );
}
}
protected override void TrackViewState()
{
base.TrackViewState();
((IStateManager)Items).TrackViewState();
}
private void ConfigureParsedItems( DropdownMenu menu )
{
foreach( MenuItem _element in menu.Items )
{
_element.SetParent( this );
_element.SetDamaoMenu( this );
_element.ElementType = _ElementType._designTimeElement;
}
}
// protected override void AddParsedSubObject(Object obj)
// {
// if (obj is MenuItem)
// {
// Items.Add((MenuItem)obj);
// }
// }
protected override void OnInit( EventArgs e )
{
base.OnInit(e);
ConfigureParsedItems( this );
}
protected override void OnPreRender(EventArgs e)
{
this.strJSToolbar.Append("<script language=javascript>\n");
this.strJSToolbar.Append("function "+this.ClientID+"_layervib(type,num){\n");
this.strJSToolbar.Append("var H=type;\n");
this.strJSToolbar.Append("var temp=(H=='visible'?'none':'')\n");
this.strJSToolbar.Append("for(var i=0;i<"+this.Items.Count+";i++){\n");
this.strJSToolbar.Append("var E=eval('document.all.[id]JokulIndex'+i+'.style');\n");
this.strJSToolbar.Append("var H=eval(i);\n");
this.strJSToolbar.Append("if(i==num){E.display=''}else{E.display=temp};\n");
this.strJSToolbar.Append("}\n");
this.strJSToolbar.Append("}\n");
this.strJSToolbar.Append("</script>");
Page.RegisterClientScriptBlock(this.ClientID+"Script",this.strJSToolbar.ToString().Replace("[id]",this.ClientID));
base.OnPreRender (e);
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
System.Text.StringBuilder strBldr = new System.Text.StringBuilder();
int i=0;
string img = "";
string strSubColor = (this.SubBackColor.Name.Length==8&&this.SubBackColor.Name.Substring(0,2)=="ff") ? this.SubBackColor.Name.Substring(2) : this.SubBackColor.Name;
string strBorderColor = (this.BorderColor.Name.Length==8&&this.BorderColor.Name.Substring(0,2)=="ff") ? this.BorderColor.Name.Substring(2) : this.BorderColor.Name;
string strSubBorderColor = (this.SubBorderColor.Name.Length==8&&this.SubBorderColor.Name.Substring(0,2)=="ff") ? this.SubBorderColor.Name.Substring(2) : this.SubBorderColor.Name;
string strForeColor = (this.ForeColor.Name.Length==8&&this.ForeColor.Name.Substring(0,2)=="ff") ? this.ForeColor.Name.Substring(2) : this.ForeColor.Name;
string strSubForeColor = (this.SubForeColor.Name.Length==8&&this.SubForeColor.Name.Substring(0,2)=="ff") ? this.SubForeColor.Name.Substring(2) : this.SubForeColor.Name;
string strMOColor = (this.MainMouseOverColor.Name.Length==8&&this.MainMouseOverColor.Name.Substring(0,2)=="ff") ? this.MainMouseOverColor.Name.Substring(2) : this.MainMouseOverColor.Name;
string strSubMOColor = (this.SubMouseOverColor.Name.Length==8&&this.SubMouseOverColor.Name.Substring(0,2)=="ff") ? this.SubMouseOverColor.Name.Substring(2) : this.SubMouseOverColor.Name;
int count = 0;
for(int k=0;k<this.Items.Count;k++)
{
if(this.Items[k].Visible)
{
count ++;
}
}
this.m_Div.BackColor = this.BackColor;
this.m_Div.Attributes.Add("id",this.ClientID+"Main");
if(base.Style.Count==0)
{
this.m_Div.Attributes.Add("style","position:relative; left:0px; top:0px; width:"+(this.ItemWidth.Value*count)+"; z-index:991");
}
else
{
this.m_Div.Attributes.Add("style","position:relative; left:"+base.Style["LEFT"]+"; top:"+base.Style["TOP"]+"; width:"+(this.ItemWidth.Value*count)+"; z-index:991");
}
this.m_Div.RenderBeginTag(output);
int subcount;
strBldr.Append("<script language='javascript'> \n");
strBldr.Append("var [id]strFrame; \n");
output.Write("<table style='CURSOR: default;COLOR: "+strForeColor+"' width=\"100%\" border='"+this.BorderWidth.Value+"' cellspacing='"+this.CellSpacing.Value+"' cellpadding='"+CellPadding+"' bordercolor='"+strBorderColor+"' background='"+this.BackGround+"'><tr> ");
foreach(MenuItem item in this.Items)
{
subcount = 0;
img = "";
if(item.Visible)
{
for(int nn=0;nn<item.Items.Count;nn++)
{
if(item.Items[nn].Visible)
{
subcount ++;
}
}
if(item.ImageUrl!=""&&this.ViewStyle == ViewStyle.IMAGE)
{
img = "<img src='"+item.ImageUrl+"' border='0'>";//style='BACKGROUND-COLOR: "+strSubColor+"'
}
if(item.Enabled)
if(item.NavigateUrl!="#")
output.Write("<td id='"+item.ID+"' align='"+this._align+"' height='"+this.Height.Value+"' width='"+this.ItemWidth.Value+"' title='"+item.ToolTip+"' class='"+item.CssClass+"' onmouseover=\""+this.ClientID+"_layervib('visible','"+i+"');this.style.backgroundColor='"+strMOColor+"';\" onmouseout=\"this.style.backgroundColor='Transparent';\"><a href='"+item.NavigateUrl+"' target='"+item.Target+"'>"+img + item.Text+"</a></td> \n");
else
output.Write("<td id='"+item.ID+"' align='"+this._align+"' height='"+this.Height.Value+"' width='"+this.ItemWidth.Value+"' title='"+item.ToolTip+"' class='"+item.CssClass+"' onmouseover=\""+this.ClientID+"_layervib('visible','"+i+"');this.style.backgroundColor='"+strMOColor+"';\" onmouseout=\"this.style.backgroundColor='Transparent';\">"+img + item.Text+"</td> \n");
else
output.Write("<td id='"+item.ID+"' align='"+this._align+"' height='"+this.Height.Value+"' width='"+this.ItemWidth.Value+"' title='"+item.ToolTip+"' class='"+item.CssClass+"'>"+img + item.Text+"</td>");
strBldr.Append("document.writeln(\"<iframe bgcolor=\\\"#000000\\\" width='"+this.ItemWidth.Value+"' frameborder=0 id='"+this.ClientID+"JokulIndex"+i+"' name='"+this.ClientID+"JokulIndex"+i+"' scrolling=no style=\\\"position:absolute; CURSOR: default; left:"+(this.CellSpacing.Value+this.BorderWidth.Value+this.ItemWidth.Value*i)+"px; top:"+((this.CellSpacing.Value+this.BorderWidth.Value)*2+this.Height.Value)+"px; z-index:9999; height: "+(subcount*(this.SubItemHeight.Value+this.SubCellSpacing.Value+this.SubBorderWidth.Value))+"px; display: none\\\"></iframe>\");");
strBldr.Append("[id]strFrame=\"<html><head><style>TABLE{font-family: Arial, Helvetica, sans-serif;font-size: 12px;}</style><link href='"+this.CssFilePath+"'></head><body><div style='POSITION:absolute; CURSOR: default; LEFT:0px; TOP:0px; Z-INDEX:992;BACKGROUND-COLOR: "+strSubColor+"' onmouseover=\\\"parent."+this.ClientID+"_layervib('visible','"+i+"')\\\" onmouseout=\\\"parent."+this.ClientID+"_layervib('visible','"+999+"')\\\">\"; ");
strBldr.Append("[id]strFrame+=\"<table style='CURSOR: default;COLOR: "+strSubForeColor+"' width='"+this.ItemWidth.Value+"' border='"+this.SubBorderWidth.Value+"' cellspacing='"+this.SubCellSpacing+"' cellpadding='"+SubCellPadding+"' bordercolor='"+strSubBorderColor+"' background='"+this.SubBackGround+"'>\"; ");
foreach(MenuSubItem subitem in item.Items)
{
img = "";
if(subitem.Visible)
{
if(this.ViewStyle == ViewStyle.IMAGE)
{
if(subitem.ImageUrl!="")
{
img = "<img src='"+subitem.ImageUrl+"' height='16' width='16' border='0'>"+subitem.Text;//<font style='FONT-WEIGHT: "+((this.SubFont.Bold==true) ? "Bold" : "")+"; FONT-SIZE: "+this.SubFont.Size.ToString()+"; COLOR: "+this.SubForeColor.Name+"; FONT-FAMILY: "+this.SubFont.Name+"; TEXT-DECORATION: "+((this.SubFont.Underline==true) ? "underline" : "")+"'>+"</font>"
}
else
{
img = "<div style='position:relative;LEFT:16px'>"+subitem.Text+"</div>";
}
}
else
{
img = subitem.Text;
}
if(subitem.Enabled)
strBldr.Append("[id]strFrame+=\"<tr><td align='"+this._subAlign+"' title='"+subitem.ToolTip+"' height='"+this.SubItemHeight.Value+"' width='100%' class='"+subitem.CssClass+"' onmouseover=\\\"this.style.backgroundColor='"+strSubMOColor+"';\\\" onmouseout=\\\"this.style.backgroundColor='Transparent';\\\"><a href='"+subitem.NavigateUrl+"' target='"+subitem.Target+"'>"+img+"</a></td></tr>\"; \n");
else
strBldr.Append("[id]strFrame+=\"<tr><td align='"+this._subAlign+"' title='"+subitem.ToolTip+"' height='"+this.SubItemHeight.Value+"' width='100%' class='"+subitem.CssClass+"' onmouseover=\\\"this.style.backgroundColor='"+strSubMOColor+"';\\\" onmouseout=\\\"this.style.backgroundColor='Transparent';\\\">"+img+"</td></tr>\"; \n");
}
}
strBldr.Append("[id]strFrame+=\"</table></div></body></html>\";");
strBldr.Append("var [id]_F=eval('window.frames."+this.ClientID+"JokulIndex"+i+"');");
strBldr.Append("[id]_F.document.writeln([id]strFrame);\n");
strBldr.Append("[id]_F.document.close();\n");
i++;
}
}
output.Write("</tr></table>");
strBldr.Append("</script>");
output.Write(strBldr.ToString().Replace("[id]",this.ClientID));
this.m_Div.RenderEndTag(output);
// base.Render(output);
// string strReg;
// strReg="<SCRIPT LANGUAGE=\"JScript\">\n";
// strReg+="var oPopup = window.createPopup();\n";
// strReg+=" function window.onload(){ \n";
// strReg+="var oPopBody = oPopup.document.body;\n";
// strReg+="oPopBody.style.backgroundColor = \"lightyellow\";\n";
// strReg+="oPopBody.style.border = \"solid black 1px\";\n";
// strReg+="oPopBody.innerHTML = \"<center><b>大毛制作</b><br>Email:damao@da-mao.com<center>\";\n";
// strReg+="oPopup.show(document.body.clientWidth/2-90,document.body.clientHeight/2-30, 180, 60, document.body);\n";
// strReg+=" } \n";
// strReg+=" function doPopupHide(){ \n";
// strReg+=" oPopup.hide(); \n";
// strReg+=" } \n";
// strReg+=" setTimeout('doPopupHide()',3000); \n";
// strReg+="</SCRIPT>\n";
// output.Write(strReg);
}
}
}
///
using System;
using System.Web.UI;
using System.Collections;
using System.ComponentModel;
namespace Damao
{
/// <summary>
/// ItemCollection 的摘要说明。
/// </summary>
public class ItemCollection : System.Collections.CollectionBase, IStateManager
{
private DropdownMenu _proprietor;
private bool _marked = false;
internal ItemCollection( DropdownMenu proprietor )
{
if( proprietor == null )
throw new ArgumentNullException();
_proprietor = proprietor;
}
#region public properties
public new int Count
{
get
{
return this.List.Count;
}
}
public bool IsSynchronized
{
get
{
return false;
}
}
public object SyncRoot
{
get
{
return this;
}
}
public MenuItem this[int index]
{
get
{
return (MenuItem)( this.List[index] );
}
set
{
this.List[index] = value;
}
}
#endregion
public DropdownMenu Proprietor
{
get
{
return _proprietor;
}
}
#region public methods
public new IEnumerator GetEnumerator()
{
return this.List.GetEnumerator();
}
public void CopyTo( Array array, int index )
{
for ( IEnumerator e = GetEnumerator(); e.MoveNext(); )
array.SetValue( e.Current, index++ );
}
public int Add( MenuItem element )
{
return AddAt( -1, element );
}
public int Add( string text )
{
return AddAt( -1, text );
}
public int AddAt( int index, MenuItem element )
{
int elementIndex = index;
if( index == -1 )
elementIndex = this.List.Add( element );
else
{
this.List.Insert( index, element );
}
ConfigureNewElement( element );
return elementIndex;
}
public int AddAt( int index, string text )
{
MenuItem _element = new MenuItem(text,"");
int elementIndex = AddAt( index, _element );
return elementIndex;
}
public int IndexOf( MenuItem element )
{
return this.List.IndexOf(element);
}
public void Remove( MenuItem element )
{
throw new NotImplementedException();
}
public new void RemoveAt( int index )
{
throw new NotImplementedException();
}
public new void Clear()
{
this.List.Clear();
}
object IStateManager.SaveViewState()
{
if( Count == 0 )
return null;
object[] membersState = new object[Count];
int index = 0;
foreach( MenuItem _element in this )
membersState[index] = new object[3] {
((IStateManager)_element).SaveViewState(),
_element.ElementType,
index++
};
return membersState;
}
void IStateManager.LoadViewState( object state )
{
if( state != null )
{
object[] membersState = (object[])state;
for( int index = 0; index < membersState.Length; index++ )
{
object[] stateElement = (object[])membersState[index];
if( stateElement[0] != null )
{
if( (byte)stateElement[1] == (byte)_ElementType._designTimeElement )
{
((IStateManager)this[index]).LoadViewState( stateElement[0] );
}
else if( (byte)stateElement[1] == (byte)_ElementType._runTimeElement )
{
// MenuItem _element = new MenuItem();
// int _elementIndex = AddAt( (int)stateElement[2], _element );
// ((IStateManager)this[_elementIndex]).TrackViewState();
// ((IStateManager)this[_elementIndex]).LoadViewState( stateElement[0] );
}
}
}
}
}
void IStateManager.TrackViewState()
{
_marked = true;
foreach( MenuItem _element in this )
((IStateManager)_element).TrackViewState();
}
bool IStateManager.IsTrackingViewState
{
get
{
return _marked;
}
}
#endregion
#region private methods
private void ConfigureNewElement( MenuItem element )
{
element.SetParent( _proprietor );
element.SetDamaoMenu( _proprietor );
element.ElementType = _ElementType._runTimeElement;
}
#endregion
}
}
////
using System;
using System.Web.UI.Design;
using System.ComponentModel;
namespace Damao
{
/// <summary>
/// MenuDesigner 的摘要说明。
/// </summary>
public class MenuDesigner : ControlDesigner
{
private DropdownMenu menu;
public override void Initialize(IComponent component)
{
this.menu = (DropdownMenu)component;
base.Initialize(component);
}
public override string GetDesignTimeHtml()
{
string _html = "";
if(this.menu.Items.Count>0)
{
System.Text.StringBuilder strB = new System.Text.StringBuilder();// style="BACKGROUND-COLOR: #ccff66"
int i=0;
string img = "";
string strColor = (this.menu.BackColor.Name.Length==8&&this.menu.BackColor.Name.Substring(0,2)=="ff") ? this.menu.BackColor.Name.Substring(2) : this.menu.BackColor.Name;
strB.Append("<div id=\""+this.menu.ClientID+"Main\" style=\"position:relative; left:0px; top:0px; width:"+(this.menu.ItemWidth.Value*this.menu.Items.Count)+"; z-index:991;BACKGROUND-COLOR: '"+strColor+"'\">");
strB.Append("<table style='CURSOR: default' width=\"100%\" border='"+this.menu.BorderWidth.Value+"' cellspacing='"+this.menu.CellSpacing.Value+"' cellpadding='"+this.menu.CellPadding+"' bordercolor='"+this.menu.BorderColor.Name+"' background='"+this.menu.BackGround+"'><tr> ");
foreach(MenuItem item in this.menu.Items)
{
img = "";
if(item.Visible)
{
if(this.menu.ViewStyle == ViewStyle.IMAGE)
{
img = "<img src='"+item.ImageUrl+"' height='16' width='16' border='0'>";
}
if(item.Enabled)
if(item.NavigateUrl!="#")
strB.Append("<td id='"+item.ID+"' align='"+this.menu.Align+"' height='"+this.menu.Height.Value+"' width='"+this.menu.ItemWidth.Value+"' title='"+item.ToolTip+"' class='"+item.CssClass+"'><a href='"+item.NavigateUrl+"' target='"+item.Target+"'>"+img + item.Text+"</a></td> ");
else
strB.Append("<td id='"+item.ID+"' align='"+this.menu.Align+"' height='"+this.menu.Height.Value+"' width='"+this.menu.ItemWidth.Value+"' title='"+item.ToolTip+"' class='"+item.CssClass+"'>"+img + item.Text+"</td> ");//
else
strB.Append("<td id='"+item.ID+"' align='"+this.menu.Align+"' height='"+this.menu.Height.Value+"' width='"+this.menu.ItemWidth.Value+"' title='"+item.ToolTip+"' class='"+item.CssClass+"'>"+img + item.Text+"</td> ");
i++;
}
}
strB.Append("</tr></table>");
strB.Append("</div>");
return strB.ToString();
}
else
{
// _html = "<table width="+this.menu.ItemWidth.Value+" border='"+this.menu.BorderWidth.Value+"' cellspacing='"+this.menu.CellSpacing.Value+"' cellpadding='"+this.menu.CellPadding.Value+"' bgcolor='"+this.menu.BackColor.Name+"' bordercolor='"+this.menu.BorderColor.Name+"'><tr><td>";
// _html+= "DropdownMenu</td></tr></table>";
_html = "<div style=\"padding:2px; background-color: ButtonFace;color:ButtonText; "+
"border-style:outset; border-width:1px; font: 75% 'Microsoft Sans Serif';\"><b>"+
"DamaoMenu</b> - "+this.menu.ID+"</div>";
}
return _html;
}
}
}
////
using System;
using System.Web.UI;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
namespace Damao
{
internal enum _ElementType : byte
{
_designTimeElement,
_runTimeElement
}
[
// ParseChildren(true),
DefaultProperty( "Items" )
]
public class MenuItem : IStateManager//,IParserAccessor
{
private DropdownMenu _parent;
private DropdownMenu _jokulMenu;
private StateBag _state;
private _ElementType _elementType;
private bool _marked = false;
private SubItemCollection _elements;
// private string _imgUrl = "";
public MenuItem()
{
}
public MenuItem(string txt , string url)
{
((IStateManager)this).TrackViewState();
this.Text = txt;
this.NavigateUrl = url;
}
#region public properties
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string ID
{
get
{
string _id = (string) ViewState["ID"];
return( ( _id == null ) ? "" : _id );
}
set
{
ViewState["ID"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string Text
{
get
{
string _text = (string) ViewState["Text"];
return( ( _text == null ) ? "" : _text );
}
set
{
ViewState["Text"] = value;
}
}
[
Browsable( true ),
PersistenceMode( PersistenceMode.InnerProperty )
]
public SubItemCollection Items
{
get
{
if( _elements == null )
_elements = new SubItemCollection( this );
return _elements;
}
}
[
Browsable( false ),
PersistenceMode( PersistenceMode.InnerDefaultProperty )
]
public DropdownMenu Parent
{
get
{
return _parent;
}
}
[
Browsable( false ),
PersistenceMode( PersistenceMode.InnerDefaultProperty )
]
public DropdownMenu DropdownMenu
{
get
{
return _jokulMenu;
}
}
[
DefaultValue( false ),
Browsable( false ),
PersistenceMode( PersistenceMode.InnerDefaultProperty )
]
public bool HasElements
{
get
{
return ( _elements != null && _elements.Count > 0 );
}
}
[
Bindable( true ),
Browsable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string Key
{
get
{
string _key = (string) ViewState["Key"];
return( ( _key == null ) ? "" : _key );
}
set
{
ViewState["Key"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string NavigateUrl
{
get
{
string _navigateUrl = (string) ViewState["NavigateUrl"];
return( ( _navigateUrl == null || _navigateUrl == "" ) ? "#" : _navigateUrl );
}
set
{
ViewState["NavigateUrl"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string Target
{
get
{
string _target = (string) ViewState["Target"];
return( ( _target == null ) ? "_parent" : _target );
}
set
{
ViewState["Target"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string ToolTip
{
get
{
string _toolTip = (string) ViewState["ToolTip"];
return( ( _toolTip == null ) ? "" : _toolTip );
}
set
{
ViewState["ToolTip"] = value;
}
}
[
Bindable( true ),
DefaultValue( true ),
PersistenceMode( PersistenceMode.Attribute )
]
public bool Enabled
{
get
{
object _enabled = ViewState["Enabled"];
return ( ( _enabled == null ) ? true : (bool)_enabled );
}
set
{
ViewState["Enabled"] = value;
}
}
[
Bindable( true ),
DefaultValue( true ),
PersistenceMode( PersistenceMode.Attribute )
]
public bool Visible
{
get
{
object _visible = ViewState["Visible"];
return ( ( _visible == null ) ? true : (bool)_visible );
}
set
{
ViewState["Visible"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string CssClass
{
get
{
string _cssClass = (string) ViewState["CssClass"];
return ( ( _cssClass == null ) ? "" : _cssClass );
}
set
{
ViewState["CssClass"] = value;
}
}
[
Bindable( true ),
PersistenceMode( PersistenceMode.Attribute ),
]
public string ImageUrl
{
get
{
string _imageUrl = (string) ViewState["ImageUrl"];
return( ( _imageUrl == null ) ? "" : _imageUrl );
// return this._imgUrl;
}
set
{
ViewState["ImageUrl"] = value;
// this._imgUrl = value;
}
}
#endregion
#region internal properties
internal StateBag ViewState
{
get
{
if( _state == null )
{
_state = new StateBag( true );
if( ((IStateManager)this).IsTrackingViewState )
((IStateManager)_state).TrackViewState();
}
return _state;
}
}
internal _ElementType ElementType
{
get
{
return _elementType;
}
set
{
_elementType = value;
}
}
#endregion
#region public methods
public MenuItem FindItem( string ID )
{
foreach( MenuItem element in Items )
{
if( element.ID == ID )
return element;
}
foreach( MenuItem element in Items )
{
MenuItem _found = element.FindItem( ID );
if( _found != null )
return _found;
}
return null;
}
object IStateManager.SaveViewState()
{
object _stateState = null;
if( _state != null )
_stateState = ((IStateManager)_state).SaveViewState();
object _elementsState = null;
if( _elements != null )
_elementsState = ((IStateManager)_elements).SaveViewState();
if ( _stateState == null && _elementsState == null )
return null;
object[] _newState = new object[2];
_newState[0] = _stateState;
_newState[1] = _elementsState;
return _newState;
}
void IStateManager.TrackViewState()
{
_marked = true;
if( _state != null )
((IStateManager)_state).TrackViewState();
if( _elements != null )
((IStateManager)_elements).TrackViewState();
}
void IStateManager.LoadViewState( object state )
{
if( state != null )
{
object[] _newState = (object[])state;
if( _newState[0] != null )
((IStateManager)ViewState).LoadViewState( _newState[0] );
if( _newState[1] != null )
((IStateManager)Items).LoadViewState( _newState[1] );
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return _marked;
}
}
#endregion
#region internal methods
// internal void SetItemID( string itemID )
// {
// ID = itemID;
// }
internal void SetParent( DropdownMenu parent )
{
_parent = parent;
}
internal void SetDamaoMenu( DropdownMenu damaoMenu )
{
_jokulMenu = damaoMenu;
}
#endregion
#region IParserAccessor 成员
// public void AddParsedSubObject(object obj)
// {
// if (obj is MenuSubItem)
// {
// Items.Add((MenuSubItem)obj);
// }
//
// }
#endregion
}
}
////////
using System;
using System.Web.UI;
using System.Collections;
using System.ComponentModel;
namespace Damao
{
[
// ParseChildren(true),
DefaultProperty( "Items" )
]
public class MenuSubItem : IStateManager
{
private MenuItem _parent;
private DropdownMenu _damaoMenu;
private StateBag _state;
private _ElementType _elementType;
private bool _marked = false;
public MenuSubItem()
{
}
public MenuSubItem(string txt , string url)
{
((IStateManager)this).TrackViewState();
this.Text = txt;
this.NavigateUrl = url;
}
#region public properties
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string ID
{
get
{
string _id = (string) ViewState["ID"];
return( ( _id == null ) ? "" : _id );
}
set
{
ViewState["ID"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string Text
{
get
{
string _text = (string) ViewState["Text"];
return( ( _text == null ) ? "" : _text );
}
set
{
ViewState["Text"] = value;
}
}
[
Browsable( false ),
PersistenceMode( PersistenceMode.InnerDefaultProperty )
]
public MenuItem Parent
{
get
{
return _parent;
}
}
[
Browsable( false ),
PersistenceMode( PersistenceMode.InnerDefaultProperty )
]
public DropdownMenu DropdownMenu
{
get
{
return _damaoMenu;
}
}
[
Bindable( true ),
Browsable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string Key
{
get
{
string _key = (string) ViewState["Key"];
return( ( _key == null ) ? "" : _key );
}
set
{
ViewState["Key"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string NavigateUrl
{
get
{
string _navigateUrl = (string) ViewState["NavigateUrl"];
return( ( _navigateUrl == null ) ? "#" : _navigateUrl );
}
set
{
ViewState["NavigateUrl"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string Target
{
get
{
string _target = (string) ViewState["Target"];
return( ( _target == null ) ? "_parent" : _target );
}
set
{
ViewState["Target"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string ToolTip
{
get
{
string _toolTip = (string) ViewState["ToolTip"];
return( ( _toolTip == null ) ? "" : _toolTip );
}
set
{
ViewState["ToolTip"] = value;
}
}
[
Bindable( true ),
PersistenceMode( PersistenceMode.Attribute )
]
public bool Enabled
{
get
{
object _enabled = ViewState["Enabled"];
return ( ( _enabled == null ) ? true : (bool)_enabled );
}
set
{
ViewState["Enabled"] = value;
}
}
[
Bindable( true ),
DefaultValue( true ),
PersistenceMode( PersistenceMode.Attribute )
]
public bool Visible
{
get
{
object _visible = ViewState["Visible"];
return ( ( _visible == null ) ? true : (bool)_visible );
}
set
{
ViewState["Visible"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string CssClass
{
get
{
string _cssClass = (string) ViewState["CssClass"];
return ( ( _cssClass == null ) ? "" : _cssClass );
}
set
{
ViewState["CssClass"] = value;
}
}
[
Bindable( true ),
DefaultValue( "" ),
PersistenceMode( PersistenceMode.Attribute )
]
public string ImageUrl
{
get
{
string _imageUrl = (string) ViewState["ImageUrl"];
return( ( _imageUrl == null ) ? "" : _imageUrl );
}
set
{
ViewState["ImageUrl"] = value;
}
}
#endregion
#region internal properties
internal StateBag ViewState
{
get
{
if( _state == null )
{
_state = new StateBag( true );
if( ((IStateManager)this).IsTrackingViewState )
((IStateManager)_state).TrackViewState();
}
return _state;
}
}
internal _ElementType ElementType
{
get
{
return _elementType;
}
set
{
_elementType = value;
}
}
#endregion
#region public methods
object IStateManager.SaveViewState()
{
object _stateState = null;
if( _state != null )
_stateState = ((IStateManager)_state).SaveViewState();
if ( _stateState == null )
return null;
return _stateState;
}
void IStateManager.TrackViewState()
{
_marked = true;
if( _state != null )
((IStateManager)_state).TrackViewState();
}
void IStateManager.LoadViewState( object state )
{
if( state != null )
{
object _newState = (object)state;
if( _newState != null )
((IStateManager)ViewState).LoadViewState( _newState );
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return _marked;
}
}
#endregion
#region internal methods
internal void SetParent( MenuItem parent )
{
_parent = parent;
}
internal void SetDamaoMenu( DropdownMenu damaoMenu )
{
_damaoMenu = damaoMenu;
}
#endregion
}
}
///////
using System;
using System.Web.UI;
using System.Collections;
using System.ComponentModel;
namespace Damao
{
/// <summary>
/// ItemCollection 的摘要说明。
/// </summary>
public class SubItemCollection : System.Collections.CollectionBase, IStateManager
{
private MenuItem _proprietor;
private bool _marked = false;
internal SubItemCollection( MenuItem proprietor )
{
if( proprietor == null )
throw new ArgumentNullException();
_proprietor = proprietor;
}
#region public properties
public new int Count
{
get
{
return this.List.Count;
}
}
public bool IsSynchronized
{
get
{
return false;
}
}
public object SyncRoot
{
get
{
return this;
}
}
public MenuSubItem this[int index]
{
get
{
return (MenuSubItem)( this.List[index] );
}
set
{
this.List[index] = value;
}
}
#endregion
public MenuItem Proprietor
{
get
{
return _proprietor;
}
}
#region public methods
public new IEnumerator GetEnumerator()
{
return this.List.GetEnumerator();
}
public void CopyTo( Array array, int index )
{
for ( IEnumerator e = GetEnumerator(); e.MoveNext(); )
array.SetValue( e.Current, index++ );
}
public int Add( MenuSubItem element )
{
return AddAt( -1, element );
}
public int Add( string text )
{
return AddAt( -1, text );
}
public int AddAt( int index, MenuSubItem element )
{
int elementIndex = index;
if( index == -1 )
elementIndex = this.List.Add( element );
else
this.List.Insert( index, element );
ConfigureNewElement( element );
return elementIndex;
}
public int AddAt( int index, string text )
{
MenuSubItem _element = new MenuSubItem(text,"");
int elementIndex = AddAt( index, _element );
return elementIndex;
}
public int IndexOf( MenuSubItem element )
{
return this.List.IndexOf(element);
}
public void Remove( MenuSubItem element )
{
throw new NotImplementedException();
}
public new void RemoveAt( int index )
{
throw new NotImplementedException();
}
public new void Clear( )
{
this.List.Clear();
}
object IStateManager.SaveViewState()
{
if( Count == 0 )
return null;
object[] membersState = new object[Count];
int index = 0;
foreach( MenuSubItem _element in this )
membersState[index] = new object[3] {
((IStateManager)_element).SaveViewState(),
_element.ElementType,
index++
};
return membersState;
}
void IStateManager.LoadViewState( object state )
{
if( state != null )
{
object[] membersState = (object[])state;
for( int index = 0; index < membersState.Length; index++ )
{
object[] stateElement = (object[])membersState[index];
if( stateElement[0] != null )
{
if( (byte)stateElement[1] == (byte)_ElementType._designTimeElement )
{
((IStateManager)this[index]).LoadViewState( stateElement[0] );
}
else if( (byte)stateElement[1] == (byte)_ElementType._runTimeElement )
{
// MenuSubItem _element = new MenuSubItem();
// int _elementIndex = AddAt( (int)stateElement[2], _element );
// ((IStateManager)this[_elementIndex]).TrackViewState();
// ((IStateManager)this[_elementIndex]).LoadViewState( stateElement[0] );
}
}
}
}
}
void IStateManager.TrackViewState()
{
_marked = true;
foreach( MenuSubItem _element in this )
((IStateManager)_element).TrackViewState();
}
bool IStateManager.IsTrackingViewState
{
get
{
return _marked;
}
}
#endregion
#region private methods
private void ConfigureNewElement( MenuSubItem element )
{
element.SetParent( _proprietor );
element.SetDamaoMenu( _proprietor.DropdownMenu );
element.ElementType = _ElementType._runTimeElement;
}
#endregion
}
}