Professional Web Applications Themes

Setting Inner Tag Content at Design Time programmatically - ASP.NET Building Controls

I am creating a web control. At design time I would like the designer to inspect the control being hosted, to see if there is any inner content and if there isn't then programmatically set the server control's markup. for example <My:Control runat="server"></My:Control> would be changed to <My:Control runat="server">Some markup</My:Control> if SetEditableDesignerRegionContent is called by the designer the markup is changed. If I call it myself, however it's not changed. The same appears to be true for Tag.SetContent and almost every other imaginable sample I've seen on the web. I have to assume that for a very basic procedure that ...

  1. #1

    Default Setting Inner Tag Content at Design Time programmatically

    I am creating a web control. At design time I would like the designer to
    inspect the control being hosted, to see if there is any inner content and if
    there isn't then programmatically set the server control's markup.
    for example <My:Control runat="server"></My:Control> would be changed to
    <My:Control runat="server">Some markup</My:Control>
    if SetEditableDesignerRegionContent is called by the designer the markup is
    changed.
    If I call it myself, however it's not changed. The same appears to be true
    for Tag.SetContent and almost every other imaginable sample I've seen on the
    web.

    I have to assume that for a very basic procedure that I am simply missing
    something. Tag.SetContent seems to do what i need it to do, except it won't
    work if I call it.

    Any tips?


    Nathaniel Guest

  2. #2

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    Based on my understanding, you have a custom server control which has
    PChildrenAttribute set to false and PersistChildrenAttribute set to
    true:

    [PChildren(false), PersistChildren(true)]
    public class Class1 : WebControl
    {
    }

    This will let user add content inside the control tag:

    <cc1:Class1 ID="Class1_1" runat="server">
    test
    </cc1:Class1>

    The objective is to add this default content when user didn't provide one.
    Please feel free to let me know if I've misunderstood anything.


    Currently I'm still researching on it and I will get back to you as soon as
    possible. Thank you for your patience and understanding.


    Sincerely,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    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 ysis and dump ysis 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.

    Walter Guest

  3. #3

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    To provide a default content in the inner tag, you need to create your own
    control designer and override the method GetPersistenceContent():

    public class Class1Designer : ControlDesigner
    {
    public override string GetPersistenceContent()
    {
    return "default content";
    }
    }

    [PChildren(false), PersistChildren(true),
    Designer(typeof(Class1Designer))]
    public class Class1 : WebControl
    {
    }


    Note this only works if the control is drag&dropped from toolbox to the
    designer surface. If the control is manually written or dropped to the
    source view, the default content will not be used. Also, when you dropped
    the control to the designer surface, you need to refresh (menu
    view/refresh, or right-click and select refresh) to see the default
    content, but the source view actually already has the default content.

    Let me know if you have any questions.


    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.


    Walter Guest

  4. #4

    Default RE: Setting Inner Tag Content at Design Time programmatically

    That is not really what I want. Im looking for a way to set this when ever
    the designer initializes and determines that there is no inner content.

    The reason for this is because there are many issues, such as recently due
    to Microsofts SP1 for VS2k5 update I can no longer drag and drop my custom
    web controls from the toolbox), that will prevent a user from being able to
    drag and drop. Also a user may wish to reset the content to default and in
    both situations the Designer has to be able to set the Inner Content. I
    cannot believe that something that Visual studio does all the time is not
    capable of being user invoked from the designer.

    "Walter Wang [MSFT]" wrote:
     
    Nathaniel Guest

  5. #5

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Any updates?

    "Nathaniel Greene" wrote:
     [/ref]
    Nathaniel Guest

  6. #6

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    Sorry for late reply. I was discussing your question with product team to
    see if there're any other approaches.

    In VS2005 the ControlDesigners effectively are only running while in design
    view. That's why the GetPersistenceContent() only is effective when the
    control is dropped on the designer surface. There're are other ways of
    customizing the content of a control when it's drag/dropped such as using
    the ToolboxDataAttribute. However this also only is effective when the
    control gets first dropped.

    Would you please tell me about your requirement? I think setting the
    default initial value will be better for user experience. I mean, if I'm
    the user and I removed the content and later found the content is filled
    with other content again, I will be surprised.


    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Guest

  7. #7

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    If you could describe your specific requirements detailly, we may find
    other approach for your objective. Thanks.

    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Guest

  8. #8

    Default RE: Setting Inner Tag Content at Design Time programmatically

    The control could most similary be compared to the "LoginView" that is part
    of the asp.net web parts.
    The control should be pre-populated with markup extracted from our
    templates. The user has to be able to customize the content however, the user
    should be able to reset the contents to the default template. Also, the
    control has to be able to be populated if the user types the control manually
    because he is unable to drag and drop.

    I can't believe that all of ms's designers can do this. This is the basis
    for everything, such as an <asp:ListItem's ListItem editing. adding columns
    to a datagrid.
    There has to be some flag or method that tells the designer to serialize the
    content now because all of ms's controls can do it.



    "Walter Wang [MSFT]" wrote:
     
    Nathaniel Guest

  9. #9

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    It seems I may have misunderstood your question at the very beginning. Are
    you referring to Region-based Editing in ASP.NET Designer support? If this
    is the case, you can find an example here:

    #Panel Container with Styled and Region-based - The Code Project - ASP.NET
    http://www.codeproject.com/useritems/shSimplePanel.asp


    Regarding "<asp:ListItem's ListItem editing", I believe you're referring to
    the CollectionEditor:

    #Collection Editor Example
    http://msdn2.microsoft.com/en-us/library/9zky1t4k.aspx


    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Guest

  10. #10

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Yes I've already seen that example but no that doesn't do what I want it to
    do.
    Yes part of the region is going to be editable, but I've already
    accomplished this.

    What I am trying to do is this.

    I have a control <my:PagesView></my:PagesView>
    When a user drags it from the toolbox or types <my:PagesView and switches to
    the designer
    there is no content in there. The Designer checks to see if there is any
    content. If there is no content it grabs the default template that shows up
    here and sticks it in here. ( This is what I'm trying to do).

    The user then makes edits to the content. Then he decides to reset the
    template because he made a mistake. So he clicks a DesignerVerb link that
    resets the content to the template default.

    I listed the other items for reference because, they can do what I can't
    seem to do.
    When you edit a collection in an Design Time, for example, adding multiple
    ListItems in an asp:DropDown - These Items are serialized INSTANTLY.

    WHY CANT I FORCE THE DESIGNER TO SERIALIZE THE CONTENTS OF MY ITEMPLATE SO
    THEY APPEAR IN THE MARKUP. Everybody else can but I can't? Why not?



    "Walter Wang [MSFT]" wrote:
     
    Nathaniel Guest

  11. #11

    Default RE: Setting Inner Tag Content at Design Time programmatically

    This code below does everything, Except one caveat.
    There is no way to force the <b> some default inner content</b> onto the aspx.
    The user has to manually click in the editable region and hit enter or
    spacebar.

    Why can VS decide when to serialize the content to the .aspx page but not
    the "designer" who is responsible for this?



    public override String
    GetEditableDesignerRegionContent(EditableDesignerR egion region)
    {
    IDesignerHost host =
    (IDesignerHost)Component.Site.GetService(typeof(ID esignerHost));
    if (host != null)
    {
    ITemplate contentTemplate;
    if (CurrentRegion== 0)
    {
    contentTemplate = gActive.ContentTemplate;
    String RetVal = ControlPersister.PersistTemplate(contentTemplate, host);
    if (RetVal.Trim().Length==0)
    {
    String InnerValue = "<b> Some default inner content</b>";
    region.Content = InnerValue;
    // ((TemplateBuilder) contentTemplate).SetTagInnerText(InnerValue);
    return InnerValue;
    // ControlPersister.PersistTemplate(
    // Tag.SetContent("hiiiiiiiiiiiiii");
    // Tag.SetDirty(true);
    }
    return RetVal;
    }
    }
    return String.Empty;
    }

    Nathaniel Guest

  12. #12

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    Sorry for delayed reply.

    Get/SetEditableDesignerRegion will not get called if you don't have regions
    in your control.

    The built-in controls you mentioned uses collection and template properties
    that are marked with [PersistenceMode(PersistenceMode.InnerProperty)]. If
    you changes the value of one of those properties and calls
    OnComponentChanged() from within your designer, this will cause the control
    to get persisted again.

    If still in doubt, please post more code of your control.

    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Guest

  13. #13

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Something like this? I've debugged it and verified that that method is being
    called however it's not serializing anything.
    }
    public override
    String GetEditableDesignerRegionContent (EditableDesignerRegion region)
    {
    IDesignerHost host1 =
    (IDesignerHost)this.GetService(typeof(IDesignerHos t));
    String TmpC =
    ControlPersister.PersistTemplate(this.gActive.Cont entTemplate, host1);
    if (TmpC.Trim().Length == 0)
    {
    TmpC = "<b>hi</b>";
    ITemplate OldValue = gActive.ContentTemplate;
    ITemplate NewValue = ControlPr.PTemplate(host1, TmpC);
    TemplateDescriptor.SetValue(this.Component, NewValue);
    // SetEditableDesignerRegionContent(region, );
    RaiseComponentChanged(TemplateDescriptor, gActive.ContentTemplate,
    NewValue);
    UpdateDesignTimeHtml();
    //ResetLayout();
    // TmpC = "<b> Default fillter Cntent that omg</b>";
    // SetEditableDesignerRegionContent(region, TmpC);
    }
    return TmpC;
    }
    Nathaniel Guest

  14. #14

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    To get the control to re-render, you just call UpdateDesignTimeHtml. To get
    it to repersist (and also re-render), call OnComponentChanged(Component,
    new ComponentChangedEventArgs(Component, null, null, null)).

    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Guest

  15. #15

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    Have you solved the issue? Would you please let me know the status of this
    post? Thanks.

    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Guest

  16. #16

    Default RE: Setting Inner Tag Content at Design Time programmatically

    That is still not doing anything. Are there certain events that it just can't
    be called in?

    I have verified that the below code is being called by placing breakpoints
    inside the if statement. I've tried your method - I then found people
    recommending using IComponentChangeService - but still no go. Am I doing
    this in the wrong event and if so is there a better event to do it in?
    public override
    String GetEditableDesignerRegionContent (EditableDesignerRegion region)
    {
    IDesignerHost host1 =
    (IDesignerHost)this.GetService(typeof(IDesignerHos t));
    String TmpC =
    ControlPersister.PersistTemplate(this.gActive.Cont entTemplate, host1);
    if (TmpC.Trim().Length == 0)
    {
    TmpC = "<b>hi</b>";
    ITemplate OldValue = gActive.ContentTemplate;
    ITemplate NewValue = ControlPr.PTemplate(host1, TmpC);
    TemplateDescriptor.SetValue(this.Component, NewValue);
    IComponentChangeService c =
    (IComponentChangeService)GetService(typeof(ICompon entChangeService));
    // this.Tag.SetContent("<ContentTemplate><b>hi</b></ContentTemplate>");
    // UpdateDesignTimeHtml();
    c.OnComponentChanged(Component, null, null, null);
    // SetEditableDesignerRegionContent(region, );
    // RaiseComponentChanged(TemplateDescriptor, gActive.ContentTemplate,
    NewValue);

    //ResetLayout();
    // TmpC = "<b> Default fillter Cntent that omg</b>";
    // SetEditableDesignerRegionContent(region, TmpC);
    }
    return TmpC;
    }

    "Walter Wang [MSFT]" wrote:
     
    Nathaniel Guest

  17. #17

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    Without full list of the objective, it seems we're having difficulty to
    understand each other clearly. Is it possible for you to create a simpler
    but complete control library, along with a short description of your
    objective and current blocking issues. With such a project, we could
    discuss more easily where to improve to achieve your objective; I can also
    give more detailed and relevant suggestion on how to improve. Thanks.

    Regards,
    Walter Wang (microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Guest

  18. #18

    Default RE: Setting Inner Tag Content at Design Time programmatically

    Hi Nathaniel,

    Have you got any further progress on this issue? We're still closely
    monitoring the thread and would be willing to continue help you on this. If
    you still feel difficult and may need some further assistance from product
    support team, I suggest you also consider contact CSS for help. Anyway,if
    there is anything else we can help, please feel to post here.

    http://msdn.microsoft.com/subscriptions/support/default.aspx

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Steven Guest

Similar Threads

  1. Setting web custom control properties at design time
    By N Kumar in forum ASP.NET Building Controls
    Replies: 4
    Last Post: February 23rd, 08:13 PM
  2. Programmatically resizing a web custom control at design time
    By ~~~ .NET Ed ~~~ in forum ASP.NET Building Controls
    Replies: 1
    Last Post: March 20th, 01:19 AM
  3. Programmatically Building an Item's Content
    By Phil Jones in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: March 1st, 01:32 AM
  4. Rendering design-time content on a web UserControl
    By malibio in forum ASP.NET Building Controls
    Replies: 2
    Last Post: February 2nd, 03:00 PM
  5. Can't read the content of a cell programmatically
    By David in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: July 3rd, 12:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139