in

ASP.NET Weblogs

Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

Follow up on composite-control pattern

I got a question on my composite-control pattern post, and I thought I'd post the answer as a blog.

Just wondering, if you were using this with more than one Label or TextBox control, would you suggest using arrays to hold them, or creating indiviual fields?

This question is best answered by knowing how they will be used. If you treat the controls as single entities, then make each one a private field. If you treat them as a group, then use an array. If you do  both then have both.

For example... if you have a composite control with 3 buttons, you'll probably want three private Button fields. If you have 15 labels, and you allow the user of your control to change the text of each label, but you also want to have 1 style that changes them all... you'll probably want to have private fields but also an array containing them all. I can't really think of a scenario right now where you wouldn't want to have the private variables, but I'm sure somebody can.

As a bit of an extension to this... If you don't know how many labels ( or whatever ) you'll have because you are generating them dynamicly... I would suggest using a Repeater instead. You get the recreation of the child controls for free when postback/viewstate comes up.

Comments

 

Nidhee said:

I have a Composite control which in turn consists of other custom server controls. The number of controls present in the Composite control is determined at runtime. Each of the child controls preserve their viewstate. I tried to put the child controls in an ArrayList within the composite control. But I cannot preserve the viewstate of the Composite Control. How can I go about doing this?
July 2, 2004 8:28 AM

Leave a Comment

(required)  
(optional)
(required)  
Add