根据书上例子自己写了一下,用VS2005写,特别方便,代码如下
usingSystem;
usingSystem.ComponentModel;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Drawing;
namespaceClingingBoy
{
[DefaultProperty("text"),
ToolboxData("<{0}:Cingmenurunat=server>")]
///
///cingmenu的摘要说明
///
publicclassCingmenu:WebControl
{
#region定义变量
privatestringtext="";
privatestringhref="";
privatestringwidth="20%";
privatestringheight="";
privatestringfontfamily="宋体";
privatestringfontsize="12px";
privatestringtarget="_self";
privatebooldisplayPipe=true;
#endregion
#region构造函数
publicCingmenu():base(HtmlTextWriterTag.Div)
{
}
#endregion
#region定义属性
[Description("该属性用于表示控件显示的文字内容"),
Bindable(true),
Category("Appearance"),
DefaultValue("")]
publicstringText
{
get
{
returntext;
}
set
{
text=value;
}
}
[Description("该属性用于表示控件的链接地址"),
Bindable(true),
Category("Appearance"),
DefaultValue("")]
publicstringHref
{
get
{
returnhref;
}
set
{
href=value;
}
}
[Description("该属性用于表示控件显示的文字大小"),
Bindable(true),
Category("Appearance"),
DefaultValue("12px")]
publicstringFontSize
{
get
{
returnfontsize;
}
set
{
fontsize=value;
}
}
[Description("该属性用于表示控件显示的文字字体类型"),
Bindable(true),
Category("Appearance"),
DefaultValue("宋体")]
publicstringFontFamily
{
get
{
returnfontfamily;
}
set
{
fontfamily=value;
}
}
[Description("该属性用于表示控件的链接的目标框架"),
Bindable(true),
Category("Appearance"),
DefaultValue("宋体")]
publicstringTarget
{
get
{
returntarget;
}
set
{
target=value;
}
}
[Description("该属性用于表示控件是否显示分隔符|"),
Bindable(true),
Category("Appearance"),
DefaultValue("true")]
publicboolDisplayPipe
{
get
{
returndisplayPipe;
}
set
{
displayPipe=value;
}
}
#endregion
protectedoverridevoidAddAttributesToRender(HtmlTextWriteroutput)
{
//定义控件div的样式style="WHITE-SPACE:nowrap;BORDER-TOP:#9991pxsolid;BACKGROUND:#f1f1f1;PADDING-BOTTOM:2px;PADDING-TOP:1px;BORDER-BOTTOM:#9991pxsolid"
output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor,"#f1f1f1");
output.AddStyleAttribute("white-space","nowrap");
output.AddStyleAttribute("BORDER-TOP","#9991pxsolid");
output.AddStyleAttribute("BORDER-BOTTOM","#9991pxsolid");
output.AddStyleAttribute("PADDING-BOTTOM","2px");
output.AddStyleAttribute("PADDING-TOP","2px");
output.AddStyleAttribute("WIDTH",width);
output.AddStyleAttribute("HEIGHT",height);
base.AddAttributesToRender(output);
}
///
///呈现控件的方法RenderContents
///
protectedoverridevoidRenderContents(HtmlTextWriteroutput)
{
//定义样式
output.AddStyleAttribute(HtmlTextWriterStyle.Width,"10px");
output.AddStyleAttribute(HtmlTextWriterStyle.FontSize,fontsize);
output.AddStyleAttribute(HtmlTextWriterStyle.FontFamily,fontfamily);
output.RenderBeginTag(HtmlTextWriterTag.Span);
//定义样式
////onmouseover="this.style.cssText=’color:black;text-decoration:none;BORDER:#9991pxsolid;DISPLAY:block;PADDING-RIGHT:6px;PADDING-LEFT:6px;PADDING-BOTTOM:2px;PADDING-TOP:2px;BACKGROUND:#ccc;’"
//style="color:black;text-decoration:none;BORDER:#f1f1f11pxsolid;DISPLAY:block;PADDING-RIGHT:6px;PADDING-LEFT:6px;PADDING-BOTTOM:2px;PADDING-TOP:2px;"href="http://localhost">文字内容
output.AddStyleAttribute(HtmlTextWriterStyle.Color,"black");
output.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration,"none");
output.AddStyleAttribute("BORDER","#f1f1f11pxsolid");
output.AddStyleAttribute("DISPLAY","block");
output.AddStyleAttribute("PADDING-RIGHT","6px");
output.AddStyleAttribute("PADDING-LEFT","6px");
output.AddStyleAttribute("PADDING-BOTTOM","2px");
output.AddStyleAttribute("PADDING-TOP","2px");
output.AddAttribute("onmouseout","this.style.cssText=’color:black;text-decoration:none;BORDER:#f1f1f11pxsolid;DISPLAY:block;PADDING-RIGHT:6px;PADDING-LEFT:6px;PADDING-BOTTOM:2px;PADDING-TOP:2px;’");
output.AddAttribute("onmouseover","this.style.cssText=’color:black;text-decoration:none;BORDER:#9991pxsolid;DISPLAY:block;PADDING-RIGHT:6px;PADDING-LEFT:6px;PADDING-BOTTOM:2px;PADDING-TOP:2px;BACKGROUND:#ccc;’");
//定义属性Href,Target
output.AddAttribute(HtmlTextWriterAttribute.Href,href);
output.AddAttribute(HtmlTextWriterAttribute.Target,target);
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write(text);
if(displayPipe)
{
output.AddStyleAttribute(HtmlTextWriterStyle.Color,"#a9a9a9");
output.AddStyleAttribute("FONT","70%");
output.AddStyleAttribute(HtmlTextWriterStyle.FontSize,fontsize);
output.AddStyleAttribute("PADDING-RIGHT","3px");
output.AddStyleAttribute("PADDING-LEFT","3px");
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("|");
output.RenderEndTag();
}
else
{
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("");
output.RenderEndTag();
}
output.RenderEndTag();
output.RenderEndTag();
//定义分隔符及样式
//使用默认逻辑来呈现子控件
base.RenderContents(output);
}
}
}
前台的
@PageLanguage="C#"%>
@RegisterAssembly="CingMenu"Namespace="ClingingBoy"TagPrefix="cc2"%>
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>测试title>
head>
<body>
<formid="form1"runat="server">
<DIV>
<TABLEid="Table5"border="0"cellpadding="0"cellspacing="0"style="height:2px">
<TR>
<TDstyle="height:26px">
<cc2:Cingmenuid="CustomControl1"DisplayPipe="true"runat="server"Target="_blank"Text="微软中国"
Href="http://www.clingingboy.com">cc2:Cingmenu>
TD>
<TDstyle="height:26px">
<cc2:Cingmenuid="Webcustomcontrol1"DisplayPipe="true"runat="server"Target="_blank"Text="微软中国"
Href="http://www.clingingboy.com">cc2:Cingmenu>
TD>
<TDstyle="height:26px">
<cc2:Cingmenuid="Webcustomcontrol2"DisplayPipe="true"runat="server"Target="_blank"Text="微软中国"
Href="http://www.clingingboy.com">cc2:Cingmenu>
TD>
<TDstyle="height:26px">
<cc2:Cingmenuid="Webcustomcontrol3"DisplayPipe="false"runat="server"Target="_blank"Text="微软中国"
Href="http://www.clingingboy.com">cc2:Cingmenu>
TD>
TR>
TABLE>
DIV>
form>
body>
html>