快速业务通道

asp.net控件设计时支持(4)

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-19
     tr.Cells.Add(tc);       t.Rows.Add(tr);       Controls.Add(t);     }   }

步骤

(1).自定义一个从CompositeControlDesigner继承的类,CompositeControlDesigner为复合控件提供设计时支持,我们需要重写CreateChildControl方法

    protected override void CreateChildControls()
    {
      base.CreateChildControls();
      Table t = (Table)myControl.Controls[0];
      if (t != null)
      {
        t.Rows[0].Cells[0].BackColor = Color.Red;
        //定义区域事件触发对象
        t.Rows[0].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] = "0";
        t.Rows[0].Cells[1].BackColor = Color.Green;
        t.Rows[0].Cells[1].Attributes[DesignerRegion.DesignerRegionAttributeName] = "1";
        t.Rows[1].Cells[0].BackColor = Color.Blue;
        t.Rows[1].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] = "2";
      }
    }

主要工作就是为Click事件定义了区域事件出发对象,方法则是为控件属性集合提供DesignerRegion.DesignerRegionAttributeName属性,注意值0,1,2和GetDesignTimeHtml方法中的DesignerRegionCollection集合顺序是一样的,不可以弄乱,按上面效果图

你可以看到0和1是触发区域事件的对象(即View1和View2),2则是编辑区域

(2)添加区域,接同上面步骤,在GetDesignTimeHtml方法中完成,这里命名哦,跟各个操作步骤紧密联系在一起的

public override String GetDesignTimeHtml(DesignerRegionCollection regions)
{
//注意构造函数命名以及区域顺序
//区域顺序与DesignerRegion.DesignerRegionAttributeName相对应
regions.Add(new DesignerRegion(this, "Header0"));
regions.Add(new DesignerRegion(this, "Header1"));
EditableDesignerRegion editableRegion =
new EditableDesignerRegion(this,
"Content" + myControl.CurrentView, false);
regions.Add(editableRegion);
//突出编辑区域
regions[myControl.CurrentView].Highlight = true;
return base.GetDesignTimeHtml();

(4)更新和获取模板数据操作

//获取模板内部数据
public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
{
IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
if (host != null)
{
ITemplate template = myControl.View1;
if (region.Name == "Content1")
template = myControl.View2;
if (template != null)
return ControlPersister.PersistTemplate(template, 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 (host != null)

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号