最近在看谭震林的道不远人 看设计时支持这一章节了 觉得文中的讲将内容从设计区域转换到aspx文件中的例子不是很好
我想做一个div 在设计区域输入文字后能加到div控件中 于是有了下面的代码
//控件
public class TheAddButoonDiv:WebControl
{
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
RenderChildren(writer);
}
}
//设计器
public class theAddDesigner :ControlDesigner
{
public override string GetDesignTimeHtml(DesignerRegionCollection regions)
{
TheAddButoonDiv div = Component as TheAddButoonDiv;
StringBuilder stringbuilder = new StringBuilder();
StringWriter writer = new StringWriter(stringbuilder);
HtmlTextWriter html = new HtmlTextWriter(writer);
regions.Add(new EditableDesignerRegion(this,"thediv"));
div.ControlStyle.AddAttributesToRender(html);
html.AddAttribute(DesignerRegion.DesignerRegionAttributeName, "0");
html.RenderBeginTag(HtmlTextWriterTag.Div);
html.RenderEndTag();
return stringbuilder.ToString();
}
public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
{
IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
if (host != null)
{
return ControlPersister.PersistControl(Component as Control, host);
}
return string.Empty;
}
public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
{
if (content == null)
{
return;
}
IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
if (content != null)
{
TheAddButoonDiv control = Component as TheAddButoonDiv;
ViewControl = null;
ViewControl =ControlParser.ParseControl(host, content);
}
}
public override bool AllowResize
{
get
{
return true;
}
}
}
不知道怎么的 老是在设计区域显示组件名称必须唯一 且不区分大小写 我想是geteditableDesignerRegionContent方法中出了问题 我想问一下怎么修改 我无法想象 如何从设计器中代替原来的控件