A note to myself (a .NET neophyte) and others who may not know how ASP.NET works:
I was writing a user control (we'll call it
ContentBoxVariation
) in ASP.NET which composes another (
ContentBox
). Both have a public property
Title
, with getters and setters.
You might call
ContentBoxVariation
in an
.aspx
page like this:
<aNamespace:ContentBoxVariation" ID="ContentBoxVariation1" Title="Welcome to Sam's" runat="server"/>
Then
ContentBoxVariation
includes
ContentBox
like this:
<aNamespace:ContentBox ID="ContentBox1" Title="<%=Title%>" runat="server"/>
You might think that the
ContentBoxVariation
would pass it's
Title
to the
ContentBox
, and that the result would be "Welcome to Sam's."
Unfortunately, it does pass it's title, but the title hasn't been changed from its default of "" at the time it happens. As far as I can tell, the instantiation happens in this order:
- Instantiate
ContentBoxVariation
- Instantiate the composed
ContentBox
- Set
ContentBox
's Title
to the variation's title, which is currently "". Even if setting it to a different variable, it seems to stay blank as if that variable doesn't exist, even though it does and no error occurs.
- Set the variation's title.
The order makes sense if you are looking at it like that, but being in templates and looking at it from that point of view, it is surprising.
The solution is to explicitly set the title for
ContentBox
in code, like this:
<%
ContentBox1.Title = this.Title;
%>
Hopefully that saves someone some time.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
Hey man :) Figured I'd post my 0.02$, jic it could help out in the future as well.
I think the main reason it didn't work as expected is due to the ASP.NET page lifecycle (
http://msdn2.microsoft.com/en-us/library/ms178472....).
All of your controls are getting initialized in the Init event. My guess is that since your ContentBoxVariation control depends on your ContentBox control the ContentBox control is being initialized prior to your ContentBoxVariation control. Which is why your Text property isn't being populated (ContentBoxVariation hasn't been initialized yet).
The msdn page says Page Load is where you'd want to set any properties. Your inline code is probably getting executed there, or after (didn't find where inline code gets executed), so that's probably why it fixed the problem. :)
Hope that helps.
Posted by
Glitch
on Jan 07, 2008 at 08:26 PM UTC - 6 hrs
Bah, misworded the above..
Page load is where you'd want to make use of properties. Not set them.. So in Page_Load you could use the Text property that was set in the tag, to say, set a label's Text property.
Hope that made my above post more clear. :)
Posted by
Glitch
on Jan 07, 2008 at 08:29 PM UTC - 6 hrs
I got it. Thanks for the link and explanations!
Posted by
Sammy Larbi
on Jan 08, 2008 at 12:35 PM UTC - 6 hrs
cool
Posted by flying
on Mar 31, 2024 at 01:56 PM UTC - 6 hrs
Leave a comment