Home All Groups Group Topic Archive Search About
Author
18 Mar 2006 12:29 AM
Jduv
When is the constructor called when a custom web control is added to an ASP.NET
page?  Here's some context so you can get a better idea of why I am asking this
question:

I am creating a custom treeview with the following attributes:

<code>
[
         Bindable(false),
         Category("Initialization"),
         DefaultValue(""),
         Description("The type of data to render in this TreeView"),
         Localizable(true)
         ]
         public virtual string ViewableDataType
         {
             get
             {
                 string s = (string)ViewState["ViewableDataType"];
                 return (s == null) ? String.Empty : s;
             }
             set
             {
                 ViewState["ViewableDataType"] = value;
             }
         }

         [
         Bindable(false),
         Category("Initialization"),
         DefaultValue(""),
         Description("Value to set the root node as"),
         Localizable(true)
         ]
         public virtual string RootName
         {
             get
             {
                 string s = (string)ViewState["RootName"];
                 return (s == null) ? String.Empty : s;
             }
             set
             {
                 ViewState["RootName"] = value;
             }
         }
</code>

You would add my treeview to a page with the following code:

<code>
<PiYD:PiYDTreeView ID="PiYDTreeView1" ViewableDataType="RegionView"
RootName="Root" runat="server" ImageSet="Arrows">
</code>

Now I would like to be able to use the values assigned to RootName and
ViewableDataType in the constructor of my TreeView.  Hence the original
question, when is the constructor called, and how can I get that data?

I am aware that this might not be possible, (i.e. the attributes are set after
the constructor call), but hopefully there is a way.

Author
19 Mar 2006 5:26 PM
CaffieneRush@gmail.com
No. According to
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx (see the
example control hierarchy source code)
the webcontrol's *default* constructor is called followed by the
setting of the webcontrol properties.

Anyway, since you're persisting property information in viewstate, you
can be certain that by the time you get to the Load event of the
lifecycle, the properties should contain the information you need both
the first time the control is loaded and on each subsequent postback.
Hint, override your webcontrol's OnLoad event handler and access your
properties safely.
Author
19 Mar 2006 5:47 PM
Jduv
CaffieneR***@gmail.com wrote:
> No. According to
> http://aspnet.4guysfromrolla.com/articles/092904-1.aspx (see the
> example control hierarchy source code)
> the webcontrol's *default* constructor is called followed by the
> setting of the webcontrol properties.
>
> Anyway, since you're persisting property information in viewstate, you
> can be certain that by the time you get to the Load event of the
> lifecycle, the properties should contain the information you need both
> the first time the control is loaded and on each subsequent postback.
> Hint, override your webcontrol's OnLoad event handler and access your
> properties safely.
>

Thanks a lot!!