My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
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:
  1. Instantiate ContentBoxVariation
  2. Instantiate the composed ContentBox
  3. 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.
  4. 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!


Comments
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 - 5 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 - 5 hrs

I got it. Thanks for the link and explanations!

Posted by Sammy Larbi on Jan 08, 2008 at 12:35 PM UTC - 5 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner