Home All Groups Group Topic Archive Search About

Composite Server Control and DefaultValue Attribute

Author
20 Feb 2006 9:00 AM
Douglas Rohm
Hi,
I'm creating a composite server control that contains a 3rd party TreeView
server control along with other properties.  I'm exposing some, not all, of
the properties of the TreeView through properties in my control.  I'm setting
the DefaultValue attribute on a property and when I drop the control onto a
web page in Design View, the default value for that property is NOT getting
set.  The really weird part is when I go to Source View of the web page and
look at the properties window for the control, it DOES show the default
value.  Does anyone know what could possibly be the problem here?  I've been
banging my head on this one for a couple of days with no resolution in sight.


Also, when I first drop the control onto the page and look in the Source
View on the web page, the property that has the DefaultValue attribute set is
not displayed in the HTML source.  Is there something else I need to do in
order for it to show up in the HTML source when the control is first dropped
onto the page?

Here is what is in the HTML source when it's initially dropped onto the page:

<cc1:Status ID="Status1" runat="server" />

What I would like is this:

<cc1:Status ID="Status1" runat="server" ServerDetailsNodeTextPaddingLeft="5"
/>

Any help would really be great.  Below is a shortened version of the control.

Thanks,

Doug

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Design;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using ExpertTreeNode = ASPNETExpert.WebControls.TreeNode;
using ExpertTree = ASPNETExpert.WebControls.ExpertTree;

namespace MyServerControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Status runat=server></{0}:Status>")]
    public class Status : CompositeControl
    {
        private string _hostName;
        private ExpertTree ServerDetailsTree;

        [
            Bindable(true),
            Category("Default"),
            RefreshProperties(RefreshProperties.Repaint),
            Description("Server hostname or IP address to query.")
        ]
        public string Hostname
        {
            get { return (string)ViewState["Hostname"]; }
            set { ViewState["Hostname"] = value; }
        }

        [
            Bindable(true),
            DefaultValue(5),
            NotifyParentProperty(true),
            Category("Server Details"),
            RefreshProperties(RefreshProperties.Repaint),
            Description("Padding to include between a node's icon and its text.")
        ]
        public int ServerDetailsNodeTextPaddingLeft
        {
            get
            {
                EnsureChildControls();
                return ServerDetailsTree.NodeTextPaddingLeft;
            }
            set
            {
                EnsureChildControls();
                ServerDetailsTree.NodeTextPaddingLeft = value;
            }
        }

        protected override void RecreateChildControls()
        {
            EnsureChildControls();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            ServerDetailsTree = new ExpertTree();
            ServerDetailsTree.ID = "ServerDetailsTree";
            ServerDetailsTree.NodeTextPaddingLeft = 5;
            this.Controls.Add(ServerDetailsTree);
        }

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

Author
20 Feb 2006 6:20 PM
DWS
Douglas,
First things first.  "page markup", "aspx", "source" or, "markup" instead of
"html" when your talking about aspx page source.

Default values are not placed in the html oh I mean the aspx page markup. 
Its by design so you can easily erase all the properties to get the defaults
back.

Doug try this one out  Walkthrough: Creating a Basic Control Designer for a
Web Server Control http://msdn2.microsoft.com/en-us/library/12yydcke.aspx

Good Luck
DWS

Show quoteHide quote
"Douglas Rohm" wrote:

> Hi,
> I'm creating a composite server control that contains a 3rd party TreeView
> server control along with other properties.  I'm exposing some, not all, of
> the properties of the TreeView through properties in my control.  I'm setting
> the DefaultValue attribute on a property and when I drop the control onto a
> web page in Design View, the default value for that property is NOT getting
> set.  The really weird part is when I go to Source View of the web page and
> look at the properties window for the control, it DOES show the default
> value.  Does anyone know what could possibly be the problem here?  I've been
> banging my head on this one for a couple of days with no resolution in sight.

>
> Also, when I first drop the control onto the page and look in the Source
> View on the web page, the property that has the DefaultValue attribute set is
> not displayed in the HTML source.  Is there something else I need to do in
> order for it to show up in the HTML source when the control is first dropped
> onto the page?
>
> Here is what is in the HTML source when it's initially dropped onto the page:
>
> <cc1:Status ID="Status1" runat="server" />
>
> What I would like is this:
>
> <cc1:Status ID="Status1" runat="server" ServerDetailsNodeTextPaddingLeft="5"
> />
>
> Any help would really be great.  Below is a shortened version of the control.
>
> Thanks,
>
> Doug
>
> using System;
> using System.Collections.Generic;
> using System.Data;
> using System.Drawing.Design;
> using System.ComponentModel;
> using System.Configuration;
> using System.Collections;
> using System.Text;
> using System.Web;
> using System.Web.Configuration;
> using System.Web.Security;
> using System.Web.UI;
> using System.Web.UI.Design;
> using System.Web.UI.WebControls;
> using System.Web.UI.WebControls.WebParts;
> using System.Web.UI.HtmlControls;
> using System.Xml;
> using ExpertTreeNode = ASPNETExpert.WebControls.TreeNode;
> using ExpertTree = ASPNETExpert.WebControls.ExpertTree;
>
> namespace MyServerControls
> {
>     [DefaultProperty("Text")]
>     [ToolboxData("<{0}:Status runat=server></{0}:Status>")]
>     public class Status : CompositeControl
>     {
>         private string _hostName;
>         private ExpertTree ServerDetailsTree;
>
>         [
>             Bindable(true),
>             Category("Default"),
>             RefreshProperties(RefreshProperties.Repaint),
>             Description("Server hostname or IP address to query.")
>         ]
>         public string Hostname
>         {
>             get { return (string)ViewState["Hostname"]; }
>             set { ViewState["Hostname"] = value; }
>         }
>
>         [
>             Bindable(true),
>             DefaultValue(5),
>             NotifyParentProperty(true),
>             Category("Server Details"),
>             RefreshProperties(RefreshProperties.Repaint),
>             Description("Padding to include between a node's icon and its text.")
>         ]
>         public int ServerDetailsNodeTextPaddingLeft
>         {
>             get
>             {
>                 EnsureChildControls();
>                 return ServerDetailsTree.NodeTextPaddingLeft;
>             }
>             set
>             {
>                 EnsureChildControls();
>                 ServerDetailsTree.NodeTextPaddingLeft = value;
>             }
>         }
>
>         protected override void RecreateChildControls()
>         {
>             EnsureChildControls();
>         }
>
>         protected override void OnLoad(EventArgs e)
>         {
>             base.OnLoad(e);
>         }
>
>         protected override void CreateChildControls()
>         {
>             Controls.Clear();
>             ServerDetailsTree = new ExpertTree();
>             ServerDetailsTree.ID = "ServerDetailsTree";
>             ServerDetailsTree.NodeTextPaddingLeft = 5;
>             this.Controls.Add(ServerDetailsTree);
>         }
>
>         protected override void Render(HtmlTextWriter writer)
>         {
>             AddAttributesToRender(writer);
>             ServerDetailsTree.RenderControl(writer);
>         }
>     }
> }
>
>
Author
20 Feb 2006 7:19 PM
Douglas Rohm
DWS,
Thanks for the tidbit on the jargon and also on the default values not
getting inserted into the markup, but I still don't know why the default
values are not getting set in the Properties window when I'm in Design View
on an aspx page.  When I go to Source View the default values show up for the
control in the Properties window, but not when I'm in Design View.  Any ideas?

Doug

Show quoteHide quote
"DWS" wrote:

> Douglas,
> First things first.  "page markup", "aspx", "source" or, "markup" instead of
> "html" when your talking about aspx page source.
>
> Default values are not placed in the html oh I mean the aspx page markup. 
> Its by design so you can easily erase all the properties to get the defaults
> back.
>
> Doug try this one out  Walkthrough: Creating a Basic Control Designer for a
> Web Server Control http://msdn2.microsoft.com/en-us/library/12yydcke.aspx
>
> Good Luck
> DWS
>
> "Douglas Rohm" wrote:
>
> > Hi,
> > I'm creating a composite server control that contains a 3rd party TreeView
> > server control along with other properties.  I'm exposing some, not all, of
> > the properties of the TreeView through properties in my control.  I'm setting
> > the DefaultValue attribute on a property and when I drop the control onto a
> > web page in Design View, the default value for that property is NOT getting
> > set.  The really weird part is when I go to Source View of the web page and
> > look at the properties window for the control, it DOES show the default
> > value.  Does anyone know what could possibly be the problem here?  I've been
> > banging my head on this one for a couple of days with no resolution in sight.
> > 
> >
> > Also, when I first drop the control onto the page and look in the Source
> > View on the web page, the property that has the DefaultValue attribute set is
> > not displayed in the HTML source.  Is there something else I need to do in
> > order for it to show up in the HTML source when the control is first dropped
> > onto the page?
> >
> > Here is what is in the HTML source when it's initially dropped onto the page:
> >
> > <cc1:Status ID="Status1" runat="server" />
> >
> > What I would like is this:
> >
> > <cc1:Status ID="Status1" runat="server" ServerDetailsNodeTextPaddingLeft="5"
> > />
> >
> > Any help would really be great.  Below is a shortened version of the control.
> >
> > Thanks,
> >
> > Doug
> >
> > using System;
> > using System.Collections.Generic;
> > using System.Data;
> > using System.Drawing.Design;
> > using System.ComponentModel;
> > using System.Configuration;
> > using System.Collections;
> > using System.Text;
> > using System.Web;
> > using System.Web.Configuration;
> > using System.Web.Security;
> > using System.Web.UI;
> > using System.Web.UI.Design;
> > using System.Web.UI.WebControls;
> > using System.Web.UI.WebControls.WebParts;
> > using System.Web.UI.HtmlControls;
> > using System.Xml;
> > using ExpertTreeNode = ASPNETExpert.WebControls.TreeNode;
> > using ExpertTree = ASPNETExpert.WebControls.ExpertTree;
> >
> > namespace MyServerControls
> > {
> >     [DefaultProperty("Text")]
> >     [ToolboxData("<{0}:Status runat=server></{0}:Status>")]
> >     public class Status : CompositeControl
> >     {
> >         private string _hostName;
> >         private ExpertTree ServerDetailsTree;
> >
> >         [
> >             Bindable(true),
> >             Category("Default"),
> >             RefreshProperties(RefreshProperties.Repaint),
> >             Description("Server hostname or IP address to query.")
> >         ]
> >         public string Hostname
> >         {
> >             get { return (string)ViewState["Hostname"]; }
> >             set { ViewState["Hostname"] = value; }
> >         }
> >
> >         [
> >             Bindable(true),
> >             DefaultValue(5),
> >             NotifyParentProperty(true),
> >             Category("Server Details"),
> >             RefreshProperties(RefreshProperties.Repaint),
> >             Description("Padding to include between a node's icon and its text.")
> >         ]
> >         public int ServerDetailsNodeTextPaddingLeft
> >         {
> >             get
> >             {
> >                 EnsureChildControls();
> >                 return ServerDetailsTree.NodeTextPaddingLeft;
> >             }
> >             set
> >             {
> >                 EnsureChildControls();
> >                 ServerDetailsTree.NodeTextPaddingLeft = value;
> >             }
> >         }
> >
> >         protected override void RecreateChildControls()
> >         {
> >             EnsureChildControls();
> >         }
> >
> >         protected override void OnLoad(EventArgs e)
> >         {
> >             base.OnLoad(e);
> >         }
> >
> >         protected override void CreateChildControls()
> >         {
> >             Controls.Clear();
> >             ServerDetailsTree = new ExpertTree();
> >             ServerDetailsTree.ID = "ServerDetailsTree";
> >             ServerDetailsTree.NodeTextPaddingLeft = 5;
> >             this.Controls.Add(ServerDetailsTree);
> >         }
> >
> >         protected override void Render(HtmlTextWriter writer)
> >         {
> >             AddAttributesToRender(writer);
> >             ServerDetailsTree.RenderControl(writer);
> >         }
> >     }
> > }
> >
> >