Rendering literal and serveral controls nested in web control at design time

S

Stephen Miller

I have a custom web control that I want to be able to render nested
literal HTML and or server controls. When deployed on an aspx page, my
control might look like:

<cc1:myControl RunAt="server">
<b>literal content</b>
<asp:RequiredFieldValidator id="valRequired" runat="server"
ErrorMessage="This field is Required."
ControlToValidate="txtRequired">*</asp:RequiredFieldValidator>
<asp:TextBox id="txtRequired" runat="server" MaxLength="50"
width="200px"></asp:TextBox>
<asp:Button id="cmdTest" runat="server" Text="Generate
event"></asp:Button>
</cc1:myControl>
<asp:Label id="lblResult" runat="server">lblResult</asp:Label>

I have figured out that at a minumum, I need to:
1. Add the design time attribute '[ParseChildren(false),
PersistChildren(true)]', which enables literal to be displayed in run
time.
2. Implement the INamingContainer interface to listen to events
generated by child controls
3. Implement the IPostBackDataHandler interface to automatically load
post back data

As such, my barebones control currently looks like:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Collections.Specialized;

namespace myCustomControl {

[ToolboxData("<{0}:myControl runat=server></{0}:myControl>")]
[ParseChildren(false), PersistChildren(true)]
public class myControl : WebControl, INamingContainer,
IPostBackDataHandler {

protected override void Render(HtmlTextWriter output){
output.Write("<b>before</b><br /><hr><br />");
base.Render(output);
output.Write("<br /><hr><br /><b>after</a>");
}

bool IPostBackDataHandler.LoadPostData(string strPostDataKey,
NameValueCollection postDataCollection) {
return true;
}
void IPostBackDataHandler.RaisePostDataChangedEvent() {}
}
}

After much effort this is currently working ok in runtime and
importantly, the nested controls can generate postback events outside
the control (ie. clicking the button cmdTest triggers a onClick event
that can be used to update lblResult).

I have now turned to design time rendering of this control and have
hit a brickwall. I would like my control to render any nested ltereral
HTML or server control placed inside the control's <cc1:myControl>
tags. Ideally I should be able to drag and drop any control from the
toolbar onto my control, and it will automagically nest the control
and add the control to the aspx page. Conceptually I should be able to
drag and drop myControl and nest within a parent myControl.

I have done a lot of reading and trawling to find a technique to
render a design time interface with little luck. Can anyone provide
some tips/code?

Is there anyhing else I should consider in building my control?

Thanks,

Stephen
 
A

Alessandro Zifiglio

hi stephen, You need to associate your control with a ControlDesigner class.
Here on this example they are using the most basic features --should put you
on the right track.
http://msdn.microsoft.com/library/d...n-us/cpguide/html/cpconhtmldesignersample.asp



Stephen Miller said:
I have a custom web control that I want to be able to render nested
literal HTML and or server controls. When deployed on an aspx page, my
control might look like:

<cc1:myControl RunAt="server">
<b>literal content</b>
<asp:RequiredFieldValidator id="valRequired" runat="server"
ErrorMessage="This field is Required."
ControlToValidate="txtRequired">*</asp:RequiredFieldValidator>
<asp:TextBox id="txtRequired" runat="server" MaxLength="50"
width="200px"></asp:TextBox>
<asp:Button id="cmdTest" runat="server" Text="Generate
event"></asp:Button>
</cc1:myControl>
<asp:Label id="lblResult" runat="server">lblResult</asp:Label>

I have figured out that at a minumum, I need to:
1. Add the design time attribute '[ParseChildren(false),
PersistChildren(true)]', which enables literal to be displayed in run
time.
2. Implement the INamingContainer interface to listen to events
generated by child controls
3. Implement the IPostBackDataHandler interface to automatically load
post back data

As such, my barebones control currently looks like:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Collections.Specialized;

namespace myCustomControl {

[ToolboxData("<{0}:myControl runat=server></{0}:myControl>")]
[ParseChildren(false), PersistChildren(true)]
public class myControl : WebControl, INamingContainer,
IPostBackDataHandler {

protected override void Render(HtmlTextWriter output){
output.Write("<b>before</b><br /><hr><br />");
base.Render(output);
output.Write("<br /><hr><br /><b>after</a>");
}

bool IPostBackDataHandler.LoadPostData(string strPostDataKey,
NameValueCollection postDataCollection) {
return true;
}
void IPostBackDataHandler.RaisePostDataChangedEvent() {}
}
}

After much effort this is currently working ok in runtime and
importantly, the nested controls can generate postback events outside
the control (ie. clicking the button cmdTest triggers a onClick event
that can be used to update lblResult).

I have now turned to design time rendering of this control and have
hit a brickwall. I would like my control to render any nested ltereral
HTML or server control placed inside the control's <cc1:myControl>
tags. Ideally I should be able to drag and drop any control from the
toolbar onto my control, and it will automagically nest the control
and add the control to the aspx page. Conceptually I should be able to
drag and drop myControl and nest within a parent myControl.

I have done a lot of reading and trawling to find a technique to
render a design time interface with little luck. Can anyone provide
some tips/code?

Is there anyhing else I should consider in building my control?

Thanks,

Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top