Custom control common html

Discussion in 'ASP .Net Building Controls' started by STech, Mar 29, 2005.

  1. STech

    STech Guest

    I would like multiple instances of a custom webcontrol that I am writing to
    be able to share some common inline html. ie. if the first custom control on
    the page has rendered this html, the remaining instances of this custom
    control should not render it.

    This would be similar to the RegisterClientScriptBlock where one can check
    Page.IsClientScriptBlockRegistered so that other controls do not render the
    same script block again.

    My question is can I use the RegisterClientScriptBlock to render this common
    inline html code and use the same Page.IsClientScriptBlockRegistered to
    check if the common inline html has been rendered or not.

    The other approach would be to store a boolean in the context which
    indicates whether the html common code has been rendered and have other
    controls check this context object.

    Would you recommend any of these methods or is there a better method. Will
    ASP.Net 2.0 break anything?

    Thanks!
     
    STech, Mar 29, 2005
    #1
    1. Advertisements

  2. Hi Stech,

    Welcome to MSDN newsgroup.
    I think your question is such a good one that I really never noticed it
    before. As far as I know, there hasn't any good means to accomplish such a
    "singleton Html section" task since we are injecting the html block in a
    custom webcontrol. And I think your suggestion on using the Page's
    Context.Items to store a flag is a good idea. In addition, we may also use
    some tricks such as add a control in the Page's Controls collection as
    flag , for example:

    protected override void Render(HtmlTextWriter output)
    {

    Label ctrl = Page.FindControl("my_flag_control") as Label;

    if(ctrl == null)
    {
    output.Write("<br>Shared Html!");
    ctrl = new Label();
    ctrl.ID = "my_flag_control";
    Page.Controls.Add(ctrl);
    }

    output.Write("<br>Main Control Content!");

    }

    But I still prefer the using Context one you mentioned since it's more
    reliable.

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Mar 29, 2005
    #2
    1. Advertisements

  3. STech

    STech Guest

    Steven,

    Thanks for the reply. As a followup, is there a way to ensure that this
    common html code we are adding is always rendered at the bottom of the page -
    ie, just before the </form> tag.

    Thanks.
     
    STech, Mar 29, 2005
    #3
  4. Hi Stech,

    Thanks for your followup. And as for ensuring a html code is at the bottom
    of the <form> section, it seems also not vaiablable in the current ASP.NET
    page model implementation(at least at custom control level). Since the
    custom control and only access limited part of its container page, and
    there is no buidlin properties or method(like the registerScriptBlock for
    script section), we can't do it in custom control's code. In addition, I
    think ensuring shared html at the bottom of <form> tag seems impossbile (at
    custom control's code) because there may exists multiple custom
    controls(some other 3rd party ones) each of them want to register such a
    block at the bottom of the form, then only one can do that. Do you think so?

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Mar 30, 2005
    #4
  5. STech

    STech Guest

    Ok. That is fine. Thanks for the reply.
     
    STech, Mar 30, 2005
    #5
  6. You're welcome.

    If you meet any further problems next time, please always feel free to post
    here.
    :--)

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Mar 31, 2005
    #6
    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.