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."
    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
    Stephen Miller, Jan 2, 2004
    #1
    1. Advertising

  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.
    http://msdn.microsoft.com/library/d...n-us/cpguide/html/cpconhtmldesignersample.asp



    "Stephen Miller" <> wrote in message
    news:...
    > 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
    Alessandro Zifiglio, Jan 2, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stan
    Replies:
    6
    Views:
    529
    John Saunders
    Aug 22, 2003
  2. =?Utf-8?B?Sm9l?=

    Rendering Server Controls in literal text

    =?Utf-8?B?Sm9l?=, Oct 24, 2005, in forum: ASP .Net
    Replies:
    14
    Views:
    677
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 24, 2005
  3. geoff

    render not rendering literal controls

    geoff, Apr 15, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    150
    Scott
    Apr 17, 2004
  4. JimmySlam
    Replies:
    1
    Views:
    104
    Evertjan.
    Apr 10, 2006
  5. Me
    Replies:
    3
    Views:
    97
    Anno Siegel
    Jan 20, 2006
Loading...

Share This Page