ASP.Net 容器控件设计器
我付出了……几天后试图弄清楚我错过了什么,我放弃了。我不知道如何让我的简单容器控件在设计器中正确显示。以下是自定义控件的基本标记:
<div>
<div>Title</div>
<div>
<!-- ASP.Net child controls -->
</div>
</div>
这是它在运行时的外观(标题,然后子控件是 GridView):
这是基本容器控件的简单代码:
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。我无法找出一个神奇的组合来为我提供一个渲染我的周围框架和子控件的设计师。
以下是使用面板作为控件的基类、无设计器以及开始/结束标记方法时设计器中的外观:
这也是我使用 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):
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方向是正确的......我做了类似的事情来制作圆形面板。只需放入面板并让渲染创建周围的 div 就变得非常容易。我可以将子控件放入其中并且它们可以正确呈现。简单地包装基本渲染方法。
网页控制(工作示例)
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)