haha2
2007-08-19 12:16 buru 阅读(90) 评论(0) 收藏 举报
第一个作品
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace AspDemo

{
css集合#region css集合
[Description("样式类")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CssNames
{

private string _tagClass = "";
[Description("未选择标签的样式")]
[NotifyParentProperty(true)]
public string TagUnSelectedClass
{
get
{ return _tagClass; }
set
{ _tagClass = value; }
}
private string _tagContentClass = "";
[Description("标签内样式")]
[NotifyParentProperty(true)]
public string TagContentClass
{
get
{ return _tagContentClass; }
set
{ _tagContentClass = value; }
}
private string _divContent = "";
[NotifyParentProperty(true)]
[Description("内容样式")]
public string DivContent
{
get
{ return _divContent; }
set
{ _divContent = value; }
}
private string _selectedClass = "";
[NotifyParentProperty(true)]
[Description("已选择标签的样式")]
public string TagSelectedClass
{
get
{ return _selectedClass; }
set
{ _selectedClass = value; }
}
}
#endregion

Template#region Template
[PersistenceMode(PersistenceMode.InnerProperty)]
public class Template
{
private ITemplate _tag = null;
private string _tagName;
[PersistenceMode(PersistenceMode.InnerProperty)]
[Description("添加控件")]
public ITemplate AddControl
{
get
{ return _tag; }
set
{ _tag = value; }
}
[Description("标签名")]
public string TagName
{
get
{ return _tagName; }
set
{ _tagName = value; }
}
}
public class TagCollection : System.Collections.ObjectModel.Collection<Template>
{
}
#endregion Template

tagControl#region tagControl
[PersistChildren(false), ParseChildren(true)]
[ToolboxData("<{0}:TagsControl runat=\"server\" height=\"200\" width=\"200\"></{0}:TagsControl>"), Designer(typeof(TagsControlDesigner))]
public class TagsControl : CompositeControl
{ 
构造函数#region 构造函数
public TagsControl()
{

}
/**//// <summary>
/// 创造一个标签
/// </summary>
/// <param name="name">标签名</param>
public TagsControl(string name)
{
Template m = new Template();
m.TagName = name;
this.Tags.Add(m);
}
#endregion

事件#region 事件
public event EventHandler Click;
public event EventHandler Reset;
public event EventHandler Submit;
protected override bool OnBubbleEvent(object source, EventArgs e)
{
bool handled = false;
if (e is CommandEventArgs)
{
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "Click")
{
OnClick(ce);
handled = true;
}
else if (ce.CommandName == "Reset")
{
OnReset(ce);
handled = true;
}
else if (ce.CommandName == "Submit")
{
OnSubmit(ce);
handled = true;
}
}
return handled;
// return base.OnBubbleEvent(source, args);
}
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
protected virtual void OnReset(EventArgs e)
{
if (Reset != null)
{
Reset(this, e);
}
}
protected virtual void OnSubmit(EventArgs e)
{
if (Submit != null)
{
Submit(this, e);
}
} 

#endregion

属性#region 属性
private TagCollection _tags;
[PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(
DesignerSerializationVisibility.Content), Category("Behavior"),Description("添加或删除标签")]
public TagCollection Tags
{
get
{
if (_tags == null) this._tags = new TagCollection();
return _tags;
}
}
//private void LabelButtons_PreRender(Object sender, EventArgs e)
//{
// this.EnsureChildControls();
//}
private CssNames css = new CssNames();
[Description("样式集合")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public CssNames Tags_CssClass
{
get
{ return css; }
}
// private int _tagsCount = 0;

private int _current = 0;
/**//// <summary>
/// 选择当前标签
/// </summary>
[Description("编辑当前标签")]
public int CurrentView
{
get
{ return _current; }
set
{ _current = value; }
}


#endregion

override CreateControls#region override CreateControls
protected override void CreateChildControls()
{
//从当前服务器控件的 ControlCollection 对象中移除所有控件。 
Controls.Clear();
CreateControlHierarchy();
//删除服务器控件的所有子控件的视图状态信息。
ClearChildViewState();

// this.ChildControlsCreated = true;
}
private void CreateControlHierarchy()
{
HtmlGenericControl BigDiv = new HtmlGenericControl("div");
BigDiv.Attributes.Add("style", String.Format("width:{0}px;height:{1}px", this.Width.Value, this.Height.Value));
// BigDiv.ID = this.ClientID;
HtmlGenericControl UpUl = new HtmlGenericControl("ul");
UpUl.Attributes.Add("id", this.UniqueID + "_ul");
// UpUl.Attributes.Add("onload", "__SetStyle(" + this.UniqueID + "_ul);");
UpUl.Attributes.Add("style", "list-style:none; margin:0");
HtmlGenericControl DownDiv = new HtmlGenericControl("div");
for (int i = 0; i < this.Tags.Count; i++)
{
HtmlGenericControl li = new HtmlGenericControl("li");
if (i == 0)
{
if (Tags_CssClass.TagSelectedClass != "")
li.Attributes.Add("class", Tags_CssClass.TagSelectedClass);
}
else if(Tags_CssClass.TagUnSelectedClass!="") li.Attributes.Add("class", Tags_CssClass.TagUnSelectedClass);
li.Attributes.Add("style", "float:left");
HtmlAnchor a = new HtmlAnchor();
a.InnerText = this.Tags[i].TagName;
a.HRef = "javascript:void(0)";
if (Tags_CssClass.TagContentClass != "")
a.Attributes.Add("class", Tags_CssClass.TagContentClass);
a.Attributes.Add("onclick", "__selectTag('" + this.UniqueID + "_tagContent" + i.ToString() + "',this,'" + this.UniqueID + "_ul','" + this.UniqueID + "_tagContent'" + ")");
a.Attributes.Add("onfocus", "this.blur()");
li.Controls.Add(a);
UpUl.Controls.Add(li);
HtmlGenericControl div = new HtmlGenericControl("div");
div.Attributes.Add("id", this.UniqueID + "_tagContent" + i.ToString());

if (i == 0) div.Attributes.Add("style", "display:block");
else div.Attributes.Add("style", "display:none");
if (this.Tags[i].AddControl != null)
{
ITemplate temp = this.Tags[i].AddControl;
this.Tags[i].AddControl.InstantiateIn(div);
}
DownDiv.Controls.Add(div);
}
BigDiv.Controls.Add(UpUl);
if (Tags_CssClass.DivContent != "")
DownDiv.Attributes.Add("class", this.Tags_CssClass.DivContent);
BigDiv.Controls.Add(DownDiv);
this.Controls.Add(BigDiv);
writeJsFunction();



//throw new Exception("The method or operation is not implemented.");
}
#endregion override CreateControls

render#region render


/**//// <summary>
/// 添加客户端js
/// </summary>
private void writeJsFunction()
{
ClientScriptManager csm = this.Page.ClientScript;
if (!csm.IsClientScriptBlockRegistered("tagsbutton"))
{
string jsStr = "function __selectTag(showContent,selfObj,id,tagContent){" +
// 操作标签
" var tag = document.getElementById(id).getElementsByTagName('li');" +
" var taglength = tag.length; " +
" for(i=0; i<taglength; i++){ " +
" tag[i].className = '';}" +

" selfObj.parentNode.className = '" + Tags_CssClass.TagSelectedClass +
// 操作内容
"'; for(i=0; j=document.getElementById(tagContent+i); i++){j.style.display = 'none';}" +

" document.getElementById(showContent).style.display = 'block'; }"; 
//"function __SetStyle(id){var tag=document.getElementById(id).getElementsByTagName('li'); this.style.height=tag[0].style.height+1+'px'; }";
// throw new Exception("The method or operation is not implemented.");
csm.RegisterClientScriptBlock(this.GetType(), "tagsbutton", jsStr, true);
}
}
#endregion

}
#endregion

Designer#region Designer
public class TagsControlDesigner : CompositeControlDesigner
{
private TagsControl myControl;
//获取tagcontrol引用
public override void Initialize(IComponent component)
{
base.Initialize(component);
myControl = component as TagsControl;
}
public override bool AllowResize
{
get
{
return true;
}
}
//使相关区域关联 --设置标识
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
HtmlGenericControl Div = myControl.Controls[0] as HtmlGenericControl;
HtmlGenericControl Ul = Div.Controls[0] as HtmlGenericControl;
HtmlGenericControl DownDiv = Div.Controls[1] as HtmlGenericControl;
double TagHeight = 20;
// double TagWidth = ;
double DivHeight =(myControl.Tags.Count>0)? (myControl.Height.Value-TagHeight):myControl.Height.Value;
double DivWidth = myControl.Width.Value;
for (int i = 0; i < Ul.Controls.Count; i++)
{
HtmlGenericControl li = Ul.Controls[i] as HtmlGenericControl;
if (myControl.Tags_CssClass.TagUnSelectedClass == "")
li.Attributes.Add("style", String.Format("float:left;width:auto;height:{0}px", TagHeight));
li.Attributes[DesignerRegion.DesignerRegionAttributeName] = i.ToString();
}
if (myControl.Tags_CssClass.DivContent == "")
DownDiv.Attributes.Add("style", String.Format("width:{0}px;height:{1}px", DivWidth, DivHeight));
DownDiv.Attributes[DesignerRegion.DesignerRegionAttributeName] = myControl.Tags.Count.ToString();
}
catch
{ }
}
//DesignerRegion区域点击事件 定义当前标签
protected override void OnClick(DesignerRegionMouseEventArgs e)
{
if (e.Region == null) return;
int t = e.Region.Name.IndexOf("tag");
if (t!= 0) return;
if (e.Region.Name.Substring(3) != myControl.CurrentView.ToString())
{
myControl.CurrentView = int.Parse(e.Region.Name.Substring(3, 1));
base.UpdateDesignTimeHtml();
}
}
//添加区域region
public override string GetDesignTimeHtml(DesignerRegionCollection regions)
{
for (int i = 0; i < myControl.Tags.Count; i++)
regions.Add(new DesignerRegion(this, "tag" + i.ToString()));
regions.Add(new EditableDesignerRegion(this, "content" + myControl.CurrentView, false));
regions[myControl.CurrentView].Highlight = true;
return base.GetDesignTimeHtml();
}
//设置编辑模板内容
public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
{
if (content == null) return;
IDesignerHost dispose = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
if (dispose != null)
{
ITemplate temp = ControlParser.ParseTemplate(dispose, content);
if (temp != null)
{ myControl.Tags[int.Parse(region.Name.Substring(7))].AddControl = temp; }
}
// base.SetEditableDesignerRegionContent(region, content);
}
//获取内容
public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
{
IDesignerHost dispose = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
if (dispose != null)
{
if (myControl.Tags.Count > 0)
{
ITemplate temp = myControl.Tags[int.Parse(region.Name.Substring(7))].AddControl;
if (temp != null)
return ControlPersister.PersistTemplate(temp, dispose);
}
}
return string.Empty;
// return base.GetEditableDesignerRegionContent(region);
}

}
#endregion Designer
}



浙公网安备 33010602011771号