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

Discussion in 'ASP .Net Building Controls' started by Stephen Miller, Jan 2, 2004.

  1. 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."
    <asp:TextBox id="txtRequired" runat="server" MaxLength="50"
    <asp:Button id="cmdTest" runat="server" Text="Generate
    <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
    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 />");
    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?


    Stephen Miller, Jan 2, 2004
    1. Advertisements

  2. 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.

    Alessandro Zifiglio, Jan 2, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.