ASP.Net 容器控件设计器

发布于 2024-10-03 13:34:06 字数 3993 浏览 4 评论 0原文

我付出了……几天后试图弄清楚我错过了什么,我放弃了。我不知道如何让我的简单容器控件在设计器中正确显示。以下是自定义控件的基本标记:

<div>
    <div>Title</div>
    <div>
        <!-- ASP.Net child controls -->
    </div>
</div>

这是它在运行时的外观(标题,然后子控件是 GridView):

alt text

这是基本容器控件的简单代码:

namespace Shoe.Controls
{
    //[Designer(typeof(ApplicationWindowDesigner))]
    //[ParseChildren(false)]
    //[PersistChildren(true)]
    [ToolboxData("<{0}:ApplicationWindow runat=\"server\"></{0}:ApplicationWindow>")]
    public class ApplicationWindow : System.Web.UI.WebControls.Panel
    {
        #region Designer Properties
        [Category("Appearance")]
        [DefaultValue("Application")]
        [Description("Title that will appear at the top of the Window.")]
        [Browsable(true)]
        /*
        public string Title
        {
            get{return (ViewState["ApplicationWindowTitle"] == null)? 
                string.Empty : 
                (string)ViewState["ApplicationWindowTitle"];}

            set{ViewState["ApplicationWindowTitle"] = value;}
        }
        */
        public string Title {get; set;}

        #endregion
        #region Rending Methods

        /*
        protected override void Render(HtmlTextWriter writer)
        {
            this.EnsureChildControls();

            writer.Write("<div class=\""+CssClass+"\" style=\"width: "+Width+"; height: "+Height+";\"><div>"+Title+"</div><div>");
            RenderChildren(writer);
            writer.Write("</div></div>");
        }
        */

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("<div class=\""+CssClass+"\" style=\"width: "+Width+"; height: "+Height+";\"><div>"+Title+"</div><div>");
            //base.RenderBeginTag(writer);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            //base.RenderEndTag(writer);
            writer.Write("</div></div>");
        }
        #endregion
    }
}

正如您在上面看到的代码,它当前基于面板控件。但是,我也尝试过仅使用 WebControl 作为基类,然后提供我自己的设计器,如下所示:

namespace Shoe.Controls
{
    public class ApplicationWindowDesigner : ContainerControlDesigner
    //public class ApplicationWindowDesigner : ControlDesigner
    {
        /*
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            SetViewFlags(ViewFlags.DesignTimeHtmlRequiresLoadComplete, true);
        }

        public override string GetDesignTimeHtml()
        {
            //return base.GetDesignTimeHtml();

            StringBuilder   sb  = new StringBuilder();
            TextWriter      s   = new StringWriter(sb);
            HtmlTextWriter  h   = new HtmlTextWriter(s);

            ApplicationWindow app_wnd = (ApplicationWindow)this.Component;

            app_wnd.RenderControl(h);

            h.Dispose();
            s.Dispose();

            return sb.ToString();
        }
        */
        /*
        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            //return base.GetDesignTimeHtml(regions);
            ApplicationWindow app_wnd = (ApplicationWindow)this.Component;

            /
            return GetDesignTimeHtml();
        }
        */
    }
}

如您所见,我尝试使用 ControlDesigner 作为基类,使用 ContainerControlDesigner 作为基类。当我使用 ControlDesigner 时,我让设计器正确渲染我的标题栏和 div;但是它不会呈现子控件(GridView)。如果我使用 ContainerControlDesigner,它会呈现子控件,但不会呈现标题栏和 div。我无法找出一个神奇的组合来为我提供一个渲染我的周围框架和子控件的设计师。

以下是使用面板作为控件的基类、无设计器以及开始/结束标记方法时设计器中的外观:

alt text

这也是我使用 WebControl 作为控件的基类并使用基于 ContainerControlDesigner 的设计器(子控件,但我的标题栏和 div 丢失)时的样子。

我缺少什么?我找到了几个 ContainerControlDesigner 的示例,但没有一个像我一样真正向周围的控件添加任何内容。

I give... after days trying to figure out what I'm missing I give up. I cannot figure out how to get my simple container control to properly display in the designer. Here is the basic markup of the custom control:

<div>
    <div>Title</div>
    <div>
        <!-- ASP.Net child controls -->
    </div>
</div>

Here is how it looks at runtime (title and then the child control is a GridView):

alt text

Here is the simple code for the basic container control:

namespace Shoe.Controls
{
    //[Designer(typeof(ApplicationWindowDesigner))]
    //[ParseChildren(false)]
    //[PersistChildren(true)]
    [ToolboxData("<{0}:ApplicationWindow runat=\"server\"></{0}:ApplicationWindow>")]
    public class ApplicationWindow : System.Web.UI.WebControls.Panel
    {
        #region Designer Properties
        [Category("Appearance")]
        [DefaultValue("Application")]
        [Description("Title that will appear at the top of the Window.")]
        [Browsable(true)]
        /*
        public string Title
        {
            get{return (ViewState["ApplicationWindowTitle"] == null)? 
                string.Empty : 
                (string)ViewState["ApplicationWindowTitle"];}

            set{ViewState["ApplicationWindowTitle"] = value;}
        }
        */
        public string Title {get; set;}

        #endregion
        #region Rending Methods

        /*
        protected override void Render(HtmlTextWriter writer)
        {
            this.EnsureChildControls();

            writer.Write("<div class=\""+CssClass+"\" style=\"width: "+Width+"; height: "+Height+";\"><div>"+Title+"</div><div>");
            RenderChildren(writer);
            writer.Write("</div></div>");
        }
        */

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("<div class=\""+CssClass+"\" style=\"width: "+Width+"; height: "+Height+";\"><div>"+Title+"</div><div>");
            //base.RenderBeginTag(writer);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            //base.RenderEndTag(writer);
            writer.Write("</div></div>");
        }
        #endregion
    }
}

As you see the code above, it is currently based off of the Panel control. However, I have also tried just using WebControl as the base class and then providing my own designer as follows:

namespace Shoe.Controls
{
    public class ApplicationWindowDesigner : ContainerControlDesigner
    //public class ApplicationWindowDesigner : ControlDesigner
    {
        /*
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            SetViewFlags(ViewFlags.DesignTimeHtmlRequiresLoadComplete, true);
        }

        public override string GetDesignTimeHtml()
        {
            //return base.GetDesignTimeHtml();

            StringBuilder   sb  = new StringBuilder();
            TextWriter      s   = new StringWriter(sb);
            HtmlTextWriter  h   = new HtmlTextWriter(s);

            ApplicationWindow app_wnd = (ApplicationWindow)this.Component;

            app_wnd.RenderControl(h);

            h.Dispose();
            s.Dispose();

            return sb.ToString();
        }
        */
        /*
        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            //return base.GetDesignTimeHtml(regions);
            ApplicationWindow app_wnd = (ApplicationWindow)this.Component;

            /
            return GetDesignTimeHtml();
        }
        */
    }
}

As you can see, I've tried using ControlDesigner as the base class and ContainerControlDesigner as the base class. When I use the ControlDesigner I get the designer to properly render my title bar and divs; however it does not render the child controls (GridView). If I use ContainerControlDesigner it renders the child controls, but not the title bar and divs. I cannot figure out the magic combination that will give me a designer that renders my surrounding frame and the child controls.

Here is what it looks like in the designer when using the Panel as the base class for the control, no designer, and the Begin / End tag approach:

alt text

This is also what it looks like when I use WebControl as the base class for the control and use a designer based off of ContainerControlDesigner (child controld, but my title bar and divs are missing).

What am I missing? I've found several examples of ContainerControlDesigner but none of them really add anything to the surrounding control like I am.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-10-10 13:34:06

你的方向是正确的......我做了类似的事情来制作圆形面板。只需放入面板并让渲染创建周围的 div 就变得非常容易。我可以将子控件放入其中并且它们可以正确呈现。简单地包装基本渲染方法。

网页控制(工作示例)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace MyExample
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:myPanel runat=server></{0}:bPanel>")]
    [Designer(typeof(MyPanelDesigner))]
    public class MyPanel : Panel
    {
        #region Property


        private string _Title = "No Title Set";
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        #endregion

        protected override void RenderChildren(HtmlTextWriter writer)
        {

            writer.Write(String.Format("<div><div>{0}</div></div>", this.Title));
            base.RenderChildren(writer);
            writer.Write("</div></div>");
        }

    }


    public class MyPanelDesigner : PanelContainerDesigner
    {
        public override string GetDesignTimeHtml(System.Web.UI.Design.DesignerRegionCollection regions)
        {
            // Read Property off control
            MyPanel c = (MyPanel)Component;
            string sTitle = c.Title;

            // Render design markup
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<div><div>{0}</div></div>", sTitle);
            sb.Append(base.GetDesignTimeHtml(regions));
            sb.AppendFormat("</div></div>");
            return sb.ToString();
        }
    }

}

Your on the right track... I do a similar thing for making rounded panels. Makes it tremendously easier just dropping in the panel and let the rendering create your surrounding divs. I'm able to drop child controls within and they render properly. Simply wrapping the base render methods.

Web Control (Working example)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace MyExample
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:myPanel runat=server></{0}:bPanel>")]
    [Designer(typeof(MyPanelDesigner))]
    public class MyPanel : Panel
    {
        #region Property


        private string _Title = "No Title Set";
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        #endregion

        protected override void RenderChildren(HtmlTextWriter writer)
        {

            writer.Write(String.Format("<div><div>{0}</div></div>", this.Title));
            base.RenderChildren(writer);
            writer.Write("</div></div>");
        }

    }


    public class MyPanelDesigner : PanelContainerDesigner
    {
        public override string GetDesignTimeHtml(System.Web.UI.Design.DesignerRegionCollection regions)
        {
            // Read Property off control
            MyPanel c = (MyPanel)Component;
            string sTitle = c.Title;

            // Render design markup
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<div><div>{0}</div></div>", sTitle);
            sb.Append(base.GetDesignTimeHtml(regions));
            sb.AppendFormat("</div></div>");
            return sb.ToString();
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文