views:

21

answers:

2

I've got a set of ASP.Net pages that display a number of asp:TextBox fields depending on the number of entries in a configuration file. I know that the number of fields won't be going above 10 or so. Given that, should I declare a sufficiently large number of text boxes in markup, or should I dynamically create the textboxes in the code-behind?

There are advantages and disadvantages to each approach, which is why I'm having trouble choosing. The advantage to the dynamic approach is that the application is more flexible - even if the number of fields goes above 10, my application will be able to scale. The disadvantage is that I'm now mixing markup and logic - my application is inserting textboxes and literals (for the labels) into the page. This will make future maintenance harder because not all of the fields are in the .aspx file.

The advantage to declaring a large form and then just showing or hiding the necessary fields is that it keeps logic and markup separate. The disadvantage, of course, is that I lose flexibility. If the number of fields goes beyond the amount I anticipate (and there is a small risk of that) I have to revisit the application to add more fields.

So, StackOverflow, which would you choose? More importantly, why would you choose your approach?

+2  A: 

One option is to combine the best of both worlds. If you put your textbox in a repeater, you can then dynamically control how many repeater rows are displayed, while having the controls statically declared.

Jason Berkan
I'm new to ASP.Net so I didn't know about the Repeater controls. Looking into it, I see that its exactly what I was looking for. Thanks!
quanticle
A: 

I would definitely go with the dynamic approach because, as you say, this will scale if requirements change in the future and you will not be sending unnecessary markup to the browser.
I don't see how using static fields avoids the issue of mixing logic and markup as you will need to use code to hide the unwanted controls.

Andy Rose