【问题标题】:Control HTML rendered by CompositeControl Templated Server Controls由 CompositeControl 模板化服务器控件呈现的控件 HTML
【发布时间】:2012-04-04 18:26:19
【问题描述】:

我正在构建我的第一个自定义服务器控件,它继承自 CompositeControl

控制的原因是能够为我们开发的多个在线应用程序提供一致的内容区域(HTML 元素)。

所以不必不断地输入:

<div class="titleBar">
</div>
<div class="actionBar">
</div>
<div class="workspace">
</div>

开发者可以按如下方式添加服务器控件:

<custom:Workspace id="..." runat="server" Title="MyTitle">
   <TitleBar>
      Here is the title
   </TitleBar>
   <ActionBar>
      <asp:button id="..." runat="server" Title="MyButton" />
   </ActionBar>
   <Content>
      <asp:DataGrid id="..." runat="server" />
   </Content>
</custom:Workspace>

我在http://msdn.microsoft.com/en-us/library/ms178657.aspx 阅读了这篇文章,它有效,但问题是......我不明白为什么。 (有没有人有一个外行版本的文章的链接,该文章描述了如何构建这些类型的服务器控件?)

到目前为止,我注意到的主要事情是 Asp.net 正在渲染一堆 SPAN 元素,这当然是我不想要的。

如何控制新的 CompositeControl 输出的 HTML?

谢谢, 雅克

PS。到目前为止,这是我的代码:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
namespace TemplatedServerControl
{
    [DefaultProperty("Title")]
    [ToolboxData("<{0}:Workspace runat=server></{0}:Workspace>")]
    public class Workspace : CompositeControl
    {
        #region FIELDS
        private ITemplate _TitleBarTemplateValue;
        private ITemplate _ActionBarTemplateValue;
        private TemplateOwner _TitleBarOwnerValue;
        private TemplateOwner _ActionBarOwnerValue;
        #endregion
        #region PROPERTY - TitleBarOwner
        [Browsable(false),
        DesignerSerializationVisibility(
        DesignerSerializationVisibility.Hidden)]
        public TemplateOwner TitleBarOwner
        {
            get
            {
                return _TitleBarOwnerValue;
            }
        } 
        #endregion
        #region PROPERTY - ActionBarOwner
        [Browsable(false),
        DesignerSerializationVisibility(
        DesignerSerializationVisibility.Hidden)]
        public TemplateOwner ActionBarOwner
        {
            get
            {
                return _ActionBarOwnerValue;
            }
        }
        #endregion
        #region PROPERTY - Title
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("[Provide the title for the workspace]")]
        [Localizable(true)]
        public string Title
        {
            get
            {
                String s = (String)ViewState["Title"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }
        #endregion
        #region PROPERTY - TitleBar
        [Browsable(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        DefaultValue(typeof(ITemplate), ""),
        Description("Control template"),
        TemplateContainer(typeof(Workspace))]
        public virtual ITemplate TitleBar
        {
            get
            {
                return _TitleBarTemplateValue;
            }
            set
            {
                _TitleBarTemplateValue = value;
            }
        }
        #endregion
        #region PROPERTY - ActionBar
        [Browsable(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        DefaultValue(typeof(ITemplate), ""),
        Description("Control template"),
        TemplateContainer(typeof(Workspace))]
        public virtual ITemplate ActionBar
        {
            get
            {
                return _ActionBarTemplateValue;
            }
            set
            {
                _ActionBarTemplateValue = value;
            }
        }
        #endregion
        #region METHOD - CreateChildControls()
        protected override void CreateChildControls()
        {
            //base.CreateChildControls();
            Controls.Clear();

            _TitleBarOwnerValue = new TemplateOwner();
            _ActionBarOwnerValue = new TemplateOwner();

            ITemplate temp1 = _TitleBarTemplateValue;
            ITemplate temp2 = _ActionBarTemplateValue;

            temp1.InstantiateIn(_TitleBarOwnerValue);
            temp2.InstantiateIn(_ActionBarOwnerValue);

            this.Controls.Add(_TitleBarOwnerValue);
            this.Controls.Add(_ActionBarOwnerValue);
        } 
        #endregion
        #region METHOD - RenderContents(HtmlTextWriter writer)
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
        } 
        #endregion
    }

    [ToolboxItem(false)]
    public class TemplateOwner : WebControl
    {
    }
}

【问题讨论】:

    标签: c# asp.net custom-server-controls composite-controls itemplate


    【解决方案1】:

    额外的&lt;span&gt; 元素来自TemplateOwner 控件,因为WebControlTemplateOwner 继承自)默认呈现&lt;span&gt; 标签。您可以更改 TemplateOwner 以指定要呈现的标签:

    public class TemplateOwner : WebControl
    {
        public TemplateOwner() :
            base(HtmlTextWriterTag.Div)
        {
        }
    }
    

    但您无需创建自己的类即可使用模板。例如,您可以只使用Panel 控件:

    private Panel _TitleBarPanel;
    private Panel _ActionBarPanel;
    
    protected override void CreateChildControls()
    {
        _TitleBarPanel = new Panel { CssClass = "titleBar" };
        _TitleBarTemplateValue.InstantiateIn(_TitleBarPanel);
        this.Controls.Add(_TitleBarPanel);
    
        _ActionBarPanel = new Panel { CssClass = "actionBar" };
        _ActionBarTemplateValue.InstantiateIn(_ActionBarPanel);
        this.Controls.Add(_ActionBarPanel);
    }
    

    【讨论】:

    • 谢谢迈克尔真的很感激。我会试一试。您是否有一个教程链接,该教程将更详细地解释复合控件和模板?
    • 我不知道有什么文章在我脑海中浮现。如果您有更多问题,请随时提问。 :-)
    【解决方案2】:

    我们使用 PlaceHolder 控件的更简单的解决方案。 与 CompositeControl 一样,这是一个容器控件。 然而,与 CompositeControl 不同的是,它根本不呈现任何内容 - 没有包含或标签。

    这确实意味着您不能像使用 CompositeControl 那样以编程方式引用整个控件,但根据您的操作,这可能不是必需的。

    每个子控件都有一个唯一的 ID,因此您可以通过编程方式引用子控件(处理事件等)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 2015-09-08
      • 1970-01-01
      • 2011-02-23
      • 2010-10-04
      • 2011-05-22
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多