C
Cameron Eckman
I am creating a custom server control where the user can add any content
similar to an ASPANEL. The custom control wraps the content in some extra
HTML above and below their content. I did not use a templated property as
it forces the developer to write more code in order to access any controls
they place within the template.
I started by inheriting from System.Web.UI.WebControls.Panel, which I do not
really want to do in the long run, and created a designer that inherited
from System.Web.UI.Design.WebControls.PanelDesigner. The control works in
the designer with the exception of not using the class names for some
reason.
What I would like is the control inherit from System.Web.UI.Control and
System.Web.UI.Design.ControlDesigner as I do not want all the extra baggage
the Panel control has. I have the control working just fine in the runtime
by just using in the Render() method and using this to render the child
controls:
foreach (Control control in this.Controls)
control.RenderControl(output);
However, I can not for the life of me figure out how to get child controls
to display in the design view. What do I need to do in the
GetDesignTimeHtml() method of the designer that I have inherited from
ControlDesigner? Or is there another designer I should use?
Thanks in advance. Here is sample code:
//// SERVER CONTROL
using System;
using System.Web.UI;
public class Section : System.Web.UI.Control // WebControls.Panel
{
private void RenderSpacer(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Nowrap, "true");
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.AddAttribute(HtmlTextWriterAttribute.Src, "/images/spacer.gif");
output.AddAttribute(HtmlTextWriterAttribute.Width, "6");
output.AddAttribute(HtmlTextWriterAttribute.Height, "1");
output.RenderBeginTag(HtmlTextWriterTag.Img);
output.RenderEndTag(); // IMG
output.RenderEndTag(); // TD
}
protected override void Render(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Class, "sectionTable");
output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
output.RenderBeginTag(HtmlTextWriterTag.Table);
output.RenderBeginTag(HtmlTextWriterTag.Tr);
// Left Spacer cell
RenderSpacer(output);
// Content cell
output.AddAttribute(HtmlTextWriterAttribute.Class, "sectionContent");
output.RenderBeginTag(HtmlTextWriterTag.Td);
foreach (Control control in this.Controls)
control.RenderControl(output);
output.RenderEndTag(); // TD
// Right Spacer cell
RenderSpacer(output);
output.RenderEndTag(); // TR
output.RenderEndTag(); // TABLE
}
}
//// DESIGNER
using System;
using System.Text;
public class SectionDesigner : System.Web.UI.Design.ControlDesigner //
WebControls.PanelDesigner
{
private string RenderSpacer()
{
StringBuilder html = new StringBuilder();
html.Append("<TD nowrap>");
html.Append("<IMG src=\"/images/spacer.gif\" width=\"6\" height=\"1\">");
html.Append("</TD>\n");
return(html.ToString());
}
public override string GetDesignTimeHtml()
{
StringBuilder html = new StringBuilder();
try
{
Section control = (Section)this.Component;
html.Append("<TABLE class=\"sectionTable\" width=\"100%\">");
html.Append("<TR>\n");
// Left Spacer cell
html.Append(RenderSpacer());
html.Append("<TD class=\"sectionContent\" width=\"100%\">");
//
//This is where the child controls should be rendered
//
html.Append("</TD>\n");
// Right Spacer cell
html.Append(RenderSpacer());
html.Append("</TR>");
html.Append("</TABLE>");
}
catch (Exception ex)
{
html.Append(base.GetErrorDesignTimeHtml(ex));
}
if ((html == null) || (html.Length == 0))
html.Append(GetEmptyDesignTimeHtml());
return(html.ToString());
}
protected override string GetEmptyDesignTimeHtml()
{
return(base.GetEmptyDesignTimeHtml());
}
}
//// WEBPAGE
<BAXWEBUI:SECTION id="scMySection" runat="server">
<ASP:LABEL id="lblSection" runat="server" text="My Label" /><BR>
<ASPANEL id="pnlButtons" runat="server">
<ASP:LINKBUTTON id="cmdBack" runat="server" text="Back"
cssclass="buttonLeft" />
<ASP:LINKBUTTON id="cmdSubmit" runat="server" text="Submit"
cssclass="button" />
<ASP:LINKBUTTON id="cmdReset" runat="server" text="Reset"
cssclass="buttonRight" />
</ASPANEL>
</BAXWEBUI:SECTION>
similar to an ASPANEL. The custom control wraps the content in some extra
HTML above and below their content. I did not use a templated property as
it forces the developer to write more code in order to access any controls
they place within the template.
I started by inheriting from System.Web.UI.WebControls.Panel, which I do not
really want to do in the long run, and created a designer that inherited
from System.Web.UI.Design.WebControls.PanelDesigner. The control works in
the designer with the exception of not using the class names for some
reason.
What I would like is the control inherit from System.Web.UI.Control and
System.Web.UI.Design.ControlDesigner as I do not want all the extra baggage
the Panel control has. I have the control working just fine in the runtime
by just using in the Render() method and using this to render the child
controls:
foreach (Control control in this.Controls)
control.RenderControl(output);
However, I can not for the life of me figure out how to get child controls
to display in the design view. What do I need to do in the
GetDesignTimeHtml() method of the designer that I have inherited from
ControlDesigner? Or is there another designer I should use?
Thanks in advance. Here is sample code:
//// SERVER CONTROL
using System;
using System.Web.UI;
public class Section : System.Web.UI.Control // WebControls.Panel
{
private void RenderSpacer(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Nowrap, "true");
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.AddAttribute(HtmlTextWriterAttribute.Src, "/images/spacer.gif");
output.AddAttribute(HtmlTextWriterAttribute.Width, "6");
output.AddAttribute(HtmlTextWriterAttribute.Height, "1");
output.RenderBeginTag(HtmlTextWriterTag.Img);
output.RenderEndTag(); // IMG
output.RenderEndTag(); // TD
}
protected override void Render(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Class, "sectionTable");
output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
output.RenderBeginTag(HtmlTextWriterTag.Table);
output.RenderBeginTag(HtmlTextWriterTag.Tr);
// Left Spacer cell
RenderSpacer(output);
// Content cell
output.AddAttribute(HtmlTextWriterAttribute.Class, "sectionContent");
output.RenderBeginTag(HtmlTextWriterTag.Td);
foreach (Control control in this.Controls)
control.RenderControl(output);
output.RenderEndTag(); // TD
// Right Spacer cell
RenderSpacer(output);
output.RenderEndTag(); // TR
output.RenderEndTag(); // TABLE
}
}
//// DESIGNER
using System;
using System.Text;
public class SectionDesigner : System.Web.UI.Design.ControlDesigner //
WebControls.PanelDesigner
{
private string RenderSpacer()
{
StringBuilder html = new StringBuilder();
html.Append("<TD nowrap>");
html.Append("<IMG src=\"/images/spacer.gif\" width=\"6\" height=\"1\">");
html.Append("</TD>\n");
return(html.ToString());
}
public override string GetDesignTimeHtml()
{
StringBuilder html = new StringBuilder();
try
{
Section control = (Section)this.Component;
html.Append("<TABLE class=\"sectionTable\" width=\"100%\">");
html.Append("<TR>\n");
// Left Spacer cell
html.Append(RenderSpacer());
html.Append("<TD class=\"sectionContent\" width=\"100%\">");
//
//This is where the child controls should be rendered
//
html.Append("</TD>\n");
// Right Spacer cell
html.Append(RenderSpacer());
html.Append("</TR>");
html.Append("</TABLE>");
}
catch (Exception ex)
{
html.Append(base.GetErrorDesignTimeHtml(ex));
}
if ((html == null) || (html.Length == 0))
html.Append(GetEmptyDesignTimeHtml());
return(html.ToString());
}
protected override string GetEmptyDesignTimeHtml()
{
return(base.GetEmptyDesignTimeHtml());
}
}
//// WEBPAGE
<BAXWEBUI:SECTION id="scMySection" runat="server">
<ASP:LABEL id="lblSection" runat="server" text="My Label" /><BR>
<ASPANEL id="pnlButtons" runat="server">
<ASP:LINKBUTTON id="cmdBack" runat="server" text="Back"
cssclass="buttonLeft" />
<ASP:LINKBUTTON id="cmdSubmit" runat="server" text="Submit"
cssclass="button" />
<ASP:LINKBUTTON id="cmdReset" runat="server" text="Reset"
cssclass="buttonRight" />
</ASPANEL>
</BAXWEBUI:SECTION>