using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace MyControlLibrary
{
[ControlBuilder(typeof(ServerTabsBuilder))]
[ParseChildren(false)]
public class ServerTabs : WebControl, INamingContainer
{
private String menuactivecss;
private String menunormalcss;
private String tabcontaincss;
private String menucontaincss;
[Category("Style")]
[DefaultValue("")]
[Description("当前显示菜单样式")]
public String MenuActiveCss
{
get
{
return menuactivecss;
}
set
{
menuactivecss = value;
}
}
[Category("Style")]
[DefaultValue("")]
[Description("未激活的菜单样式")]
public String MenuNormalCss
{
get
{
return menunormalcss;
}
set
{
menunormalcss = value;
}
}
[Category("Style")]
[DefaultValue("")]
[Description("容器样式")]
public String TabContainCss
{
get
{
return tabcontaincss;
}
set
{
tabcontaincss = value;
}
}
[Category("Style")]
[DefaultValue("")]
[Description("菜单容器样式")]
public String MenuContainCss
{
get
{
return menucontaincss;
}
set
{
menucontaincss = value;
}
}
protected override void OnPreRender(EventArgs e)
{
String script = "<script>";
script += ("var count=" + this.Controls.Count.ToString() + ";");
script += "function swichtab(tab,index){";
script += "for(i=0;i<count;i++){";
script += ("var viewtab=document.getElementById('"+this.ClientID + "panel'" +"+i);");
script += ("var viewtitle=document.getElementById('" + this.ClientID + "menu'" + "+i);");
script += "if(i==index){";
script += ("viewtitle.className='"+MenuActiveCss+"';");
script += "viewtab.style.display = 'block';";
script += "}else{";
script += "viewtitle.className='"+MenuNormalCss+"';";
script += "viewtab.style.display='none';}";
script += "};";
script += "}";
script += "</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"MyScript", script);
base.OnPreRender(e);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class,MenuContainCss);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.RenderBeginTag(HtmlTextWriterTag.Ul);
for (int i = 0; i < this.Controls.Count; i++)
{
ServerTab tab = (ServerTab)this.Controls[i];
writer.AddStyleAttribute(HtmlTextWriterStyle.Display,"block");
writer.AddStyleAttribute("float","left");
writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");
if (0== i)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, MenuActiveCss);
}
else
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, MenuNormalCss);
}
writer.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID+"menu"+i.ToString());
writer.AddAttribute("onmouseover", "swichtab(this,"+i.ToString()+")");
writer.RenderBeginTag(HtmlTextWriterTag.Li);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(tab.Text);
writer.RenderEndTag();
writer.RenderEndTag();
}
writer.RenderEndTag();
writer.RenderEndTag();
for (int i = 0; i < this.Controls.Count; i++)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, TabContainCss);
if (0 != i)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.Display,"none") ;
}
writer.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID+"panel"+i.ToString() );
writer.RenderBeginTag(HtmlTextWriterTag.Div);
this.Controls[i].RenderControl(writer);
writer.RenderEndTag();
}


}


protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
protected override void AddParsedSubObject(object obj)
{
if (obj is ServerTab)
base.AddParsedSubObject(obj);
}
}


//自定义页分析器
public class ServerTabsBuilder : ControlBuilder
{
public override Type GetChildControlType(string tagName, IDictionary attribs)
{
if (String.Compare(tagName, "tab", true) == 0)
return typeof(ServerTab);
else
return null;
}
}
[ToolboxItem(false)]
public class ServerTab : Control
{
private string _Text;
public string Text
{
get { return _Text; }
set { _Text = value; }
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="MyControlLibrary" Namespace="MyControlLibrary" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<style type="text/css">
div
{
line-height: 150%;
margin: 0 auto;
text-align: left;
}
ul, li
{
list-style-type: none;
margin: 0px;
padding: 0px;
}
.tabbox{
HEIGHT:205px;
width:480px;
border:1px;
}
.menu-1
{
border-right: 1px solid #DCE4ED;
height: 29px;
width: 480px;
float: left;
}
.menu-1 li
{
display: block;
text-align: center;
height: 30px !important;
height: 27px;
line-height: 30px !important;
line-height: 27px;
padding-top: 3px;
width: 160px;
float: left;
cursor: pointer;
}
.menu-1-active
{
float:left;
text-align: center;
height: 30px !important;
height: 27px;
line-height: 30px !important;
line-height: 27px;
padding-top: 3px;
width: 160px;
background: url(http://www.blogcn.com/images/door-bg-1.gif) no-repeat left top;
color:#ff00ff;
}
.menu-1-normal
{
float: left;
text-align: center;
height: 30px !important;
height: 27px;
line-height: 30px !important;
line-height: 27px;
padding-top: 3px;
width: 160px;
background: url(http://www.blogcn.com/images/door-bg-2.gif) no-repeat left top;
}
.menu-2
{
height: 170px;
width: 479px;
float: left;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-right-color: #DCE4ED;
border-bottom-color: #DCE4ED;
border-left-color: #DCE4ED;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:ServerTabs ID="ServerTabs1" runat="server"
Height="205px" Width="480px"
TabContainCss="menu-2" MenuActiveCss="menu-1-active" MenuNormalCss="menu-1-normal" MenuContainCss="menu-1"
><cc1:ServerTab runat="server" Text="First Tab">
<asp:Label runat="server" Text="Label" ID="Label2"></asp:Label>
Contents of the first tab
<asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
</cc1:ServerTab>
<cc1:ServerTab runat="server" Text="Second Tab">
Contents of the second tab
</cc1:ServerTab>
<cc1:ServerTab runat="server" Text="Third Tab">
Contents of the third tab
</cc1:ServerTab>
</cc1:ServerTabs>
</div>
</form>
</body>
</html>

。
发表于 @ 2007年05月20日 02:26:00 | 评论( loading... ) | 举报| 收藏