Complex properties (Button) not persisting in webcontrol

Discussion in 'ASP .Net Building Controls' started by Phillyboy123, Nov 18, 2003.

  1. Phillyboy123

    Phillyboy123 Guest

    This seems to be a common issue and one that I have not found a
    solution to. What I want to do is theoreticly simple. I have tried
    creating webcontrols with simple properties string e.t.c and all work
    fine. I am now in the process if trying to create a webcontrol that
    contains more complex properties. In this case a Property of type
    Button. I've played around with all of the recomended metadata
    ParseChildren(true/false), PersistChildren(true/false),
    PersistanceMode e.t.c. I've also been using "Developing Microsoft
    ASP.NET server controls and components" by Nikhil Kothari and Vandana
    Datye" but with no success in this area.

    At designtime the control is rendered on the page correctly and the
    Properties of the Button within the custom control appear in the
    properties toolbar/palette. Changes to the Buttons properties ie. Text
    or BackColor seem to take effect when the page is redrawn however
    these properties do not persist.

    1. There values are lost when the project is rebuilt or ran.
    2. The HTML for the aspx page has not changed to reflect the new
    property values.

    Below is the code for the class.

    using System;
    using System.Collections;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Web.UI.Design;

    namespace DynamicWebControls.Controls
    {
    public class TestControl:
    System.Web.UI.WebControls.WebControl,INamingContainer
    {
    Button _myButton;

    protected override void CreateChildControls()
    {
    Controls.Clear();
    Controls.Add(MyButton);
    }

    [
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    NotifyParentProperty(true),
    PersistenceMode(PersistenceMode.InnerProperty)
    ]
    public Button MyButton
    {
    get
    {
    EnsureChildControls();
    if(_myButton==null)_myButton=new Button();

    return _myButton;
    }
    set
    {
    _myButton = value;
    }
    }

    protected override void Render(HtmlTextWriter writer)
    {
    MyButton.RenderControl(writer);
    }
    }
    }

    Thanks in advance for your help.

    Phil.
     
    Phillyboy123, Nov 18, 2003
    #1
    1. Advertising

  2. Phillyboy123

    Sam Fields Guest

    Correct me if I'm wrong, but it seems that you're just looking to customize
    the <asp:Button>. You may want to consider inheriting the button class
    instead of wrapping it.

    Thanks,

    Sam Fields

    "Phillyboy123" <> wrote in message
    news:...
    > This seems to be a common issue and one that I have not found a
    > solution to. What I want to do is theoreticly simple. I have tried
    > creating webcontrols with simple properties string e.t.c and all work
    > fine. I am now in the process if trying to create a webcontrol that
    > contains more complex properties. In this case a Property of type
    > Button. I've played around with all of the recomended metadata
    > ParseChildren(true/false), PersistChildren(true/false),
    > PersistanceMode e.t.c. I've also been using "Developing Microsoft
    > ASP.NET server controls and components" by Nikhil Kothari and Vandana
    > Datye" but with no success in this area.
    >
    > At designtime the control is rendered on the page correctly and the
    > Properties of the Button within the custom control appear in the
    > properties toolbar/palette. Changes to the Buttons properties ie. Text
    > or BackColor seem to take effect when the page is redrawn however
    > these properties do not persist.
    >
    > 1. There values are lost when the project is rebuilt or ran.
    > 2. The HTML for the aspx page has not changed to reflect the new
    > property values.
    >
    > Below is the code for the class.
    >
    > using System;
    > using System.Collections;
    > using System.Web;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > using System.Web.UI.Design;
    >
    > namespace DynamicWebControls.Controls
    > {
    > public class TestControl:
    > System.Web.UI.WebControls.WebControl,INamingContainer
    > {
    > Button _myButton;
    >
    > protected override void CreateChildControls()
    > {
    > Controls.Clear();
    > Controls.Add(MyButton);
    > }
    >
    > [
    >

    DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    > NotifyParentProperty(true),
    > PersistenceMode(PersistenceMode.InnerProperty)
    > ]
    > public Button MyButton
    > {
    > get
    > {
    > EnsureChildControls();
    > if(_myButton==null)_myButton=new Button();
    >
    > return _myButton;
    > }
    > set
    > {
    > _myButton = value;
    > }
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > MyButton.RenderControl(writer);
    > }
    > }
    > }
    >
    > Thanks in advance for your help.
    >
    > Phil.
     
    Sam Fields, Jan 15, 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. JezB
    Replies:
    1
    Views:
    372
  2. Alessandro Zifiglio

    Persisting properties in templates and not the control itself

    Alessandro Zifiglio, Nov 9, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    179
    Alessandro Zifiglio
    Nov 9, 2003
  3. Steve Barker

    Properties not persisting at design time.

    Steve Barker, Jan 22, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    160
    Steve Barker
    Jan 22, 2004
  4. Replies:
    1
    Views:
    304
    Tim Dot NoSpam
    May 19, 2006
  5. Phillyboy123

    Complex properties (Button) not persisting in webcontrol

    Phillyboy123, Nov 19, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    144
    Phillyboy123
    Nov 19, 2003
Loading...

Share This Page