Custom control that can contain other controls

Discussion in 'ASP .Net' started by Nick Gilbert, Jan 8, 2007.

  1. Nick Gilbert

    Nick Gilbert Guest

    Hi,

    How can I create a custom control which will wrap its content in a
    header and footer?

    eg:

    Is it possible to create a .NET user control which can surround other
    controls? eg:
    <my:RoundedCornerBox id=foo runat=server>
    Content1
    Content2
    </my:RoundedCornerBox>

    renders as:

    <div class=foobar>
    Content.
    Content.
    </div>

    I don't think this is possible with Web User Controls.

    Thanks,

    Nick...
    PS the above example is vastly simplified but demonstrates the concept
    what I need to do.
     
    Nick Gilbert, Jan 8, 2007
    #1
    1. Advertising

  2. Nick Gilbert

    Mark Rae Guest

    "Nick Gilbert" <> wrote in message
    news:...

    > I don't think this is possible with Web User Controls.


    If you mean something like this: http://www.markrae.com/contact/quote.aspx

    then it most certainly is possible... :)

    Is that what you mean...?
     
    Mark Rae, Jan 8, 2007
    #2
    1. Advertising

  3. Nick Gilbert

    Nick Gilbert Guest

    > If you mean something like this: http://www.markrae.com/contact/quote.aspx
    >
    > then it most certainly is possible... :)
    >
    > Is that what you mean...?


    Yes that's almost exactly what I need - just in green :)

    How did you do that?

    Nick...
     
    Nick Gilbert, Jan 8, 2007
    #3
  4. Nick Gilbert

    Nick Gilbert Guest

    > If you mean something like this: http://www.markrae.com/contact/quote.aspx
    >
    > then it most certainly is possible... :)
    >
    > Is that what you mean...?


    Yes that's almost exactly what I need - just in green :)

    How did you do that?

    Nick...
     
    Nick Gilbert, Jan 8, 2007
    #4
  5. Nick Gilbert

    Mark Rae Guest

    "Nick Gilbert" <> wrote in message
    news:...

    >> If you mean something like this:
    >> http://www.markrae.com/contact/quote.aspx
    >>
    >> then it most certainly is possible... :)
    >>
    >> Is that what you mean...?

    >
    > Yes that's almost exactly what I need - just in green :)
    >
    > How did you do that?


    It's a usercontrol originally developed by Scott Mitchell which I modified
    to produce XHTML-compliant markup.

    You can download it from the 4GuysFromRolla site:
    http://aspnet.4guysfromrolla.com/articles/081104-1.aspx
     
    Mark Rae, Jan 8, 2007
    #5
  6. Nick Gilbert

    Nick Gilbert Guest

    > It's a usercontrol originally developed by Scott Mitchell which I modified
    > to produce XHTML-compliant markup.


    Thanks for the link.

    I'm actually quoted on that page "Special thanks to Nick Gilbert for
    helping..."

    You would think I would remember wouldn't you?! I must have had too
    much alcohol over Christmas! :)

    Thanks,

    Nick...
     
    Nick Gilbert, Jan 8, 2007
    #6
  7. Nick Gilbert

    Mark Rae Guest

    "Nick Gilbert" <> wrote in message
    news:%...

    >> It's a usercontrol originally developed by Scott Mitchell which I
    >> modified to produce XHTML-compliant markup.

    >
    > Thanks for the link.
    >
    > I'm actually quoted on that page "Special thanks to Nick Gilbert for
    > helping..."
    >
    > You would think I would remember wouldn't you?! I must have had too much
    > alcohol over Christmas! :)


    ROTFLMAO!!!
     
    Mark Rae, Jan 8, 2007
    #7
  8. Nick Gilbert

    Nick Gilbert Guest

    I've just remembered why I don't use that code. You can't specify the
    corner images and so are restricted to very basic designs. Also it
    renders using tables - which makes for very heavy HTML if you have lots
    of boxes on one page. But hopefully I can use the code to find out how
    to write my "wrapper" control that will mark things up as a box with
    rounded corners.

    Currently, my code to make a rounded corner box doesn't use any tables
    and the amount of HTML per box is very small.

    Nick...
     
    Nick Gilbert, Jan 8, 2007
    #8
  9. Nick Gilbert

    Mark Rae Guest

    "Nick Gilbert" <> wrote in message
    news:...

    > You can't specify the corner images and so are restricted to very basic
    > designs.


    You don't need to specify the images - it creates them dynamically (if they
    don't already exist) according to the settings you choose - it's totally
    flexible in that way...

    > Also it renders using tables - which makes for very heavy HTML if you have
    > lots of boxes on one page.


    I have to say that I don't find it heavy at all...

    > Currently, my code to make a rounded corner box doesn't use any tables and
    > the amount of HTML per box is very small.


    Well, I'd certainly be interested to see that...

    I believe rounded corners is something being considered for the next
    standardisation of CSS...
     
    Mark Rae, Jan 8, 2007
    #9
  10. Hello Nick,

    I'm not quite sure about what your wrap control will look like(for UI
    appearance), however, for building such a custom control through available
    ASP.NET control development features, here are some suggestions:

    1. I think ascx usercontrol is not suitable here, and a custom web server
    control is preferred.

    2. As what you need is the following like control, the control itself can
    contain discretionary child content(normal html or other server controls):

    =========
    <my:RoundedCornerBox id=foo runat=server>
    Content1
    Content2
    </my:RoundedCornerBox>
    ===========

    and after rendering, all the child content will be also wrapped in
    container html set such as

    ===========
    <div>
    child content rendering...
    </div>
    ===========

    Thus, I think a good choice is to use ASP.NET custom template control(just
    like the LoginView control...) which can let you define a custom inner
    template, and those child content in the template will always be rendered
    within the container(you determined in your toplevel control's control
    creation code logic).

    Here are some MSDN reference and web article introduce developing custom
    template control.

    #Building Templated Custom ASP.NET Server Controls
    http://msdn2.microsoft.com/en-us/library/aa478964.aspx

    #Creating a Templated Control
    http://www.samspublishing.com/articles/article.asp?p=101748&seqNum=7&rl=1

    For your scenario, you can define a Template property for your custom wrap
    control(which use to contain any content you want to include), then in your
    control's main control creation codelogic, you first create the outside
    wrapper html content(<div>, <table> or ...) and add the template instanced
    sub controls into the outside wrapper. Also, you can use multi-templates if
    necessary, it's quite flexible.

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 9, 2007
    #10
  11. Nick Gilbert

    Nick Gilbert Guest

    Hi,

    I have managed to do what I want by making my own control like this:

    [DefaultProperty("Text"), PersistChildren(true), ParseChildren(false)]
    public class RoundedCornerBox : System.Web.UI.WebControls.WebControl {
    public override void RenderBeginTag(HtmlTextWriter writer) {
    writer.Write("(START HTML)");
    }

    public override void RenderEndTag(HtmlTextWriter writer) {
    writer.Write("(END HTML)");
    }
    }


    I'm not sure if this is the best way of doing it, but it seems to work OK.

    Nick...
     
    Nick Gilbert, Jan 10, 2007
    #11
  12. Thanks for your followup Nick,

    Glad that you've found a solution. BTW, if you have interests try the
    template control approach and if there is anything else we can help, please
    feel free to post here.

    Have a good day!

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 11, 2007
    #12
    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. Jonah Olsson
    Replies:
    1
    Views:
    588
    Scott Allen
    Apr 5, 2005
  2. Custom controls that contain other controls

    , Apr 26, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    153
    Brock Allen
    Apr 26, 2005
  3. Jonah Olsson
    Replies:
    2
    Views:
    211
    Jonah Olsson
    Apr 5, 2005
  4. Roger Pack
    Replies:
    3
    Views:
    166
    Roger Pack
    Sep 28, 2010
  5. Jason Carlton
    Replies:
    11
    Views:
    251
    Dr J R Stockton
    Dec 8, 2009
Loading...

Share This Page