Repeating question: Is it better to create one label and update its text from code-behind, or create multiple labels and show/hide the label as needed?
Case: one label, update from code-behind
- There is only one label created on the aspx page, which keeps the page light-weight.
- The code remains clean on .aspx page, as well as the code-behind page.
- Chances of wrong or multiple text/ labels displaying is minimized.
Case: multiple labels with texts, show/hide label from code-behind
- All the possible texts is already visible from the .aspx page; updating does not require re-compiling.
I see merits and demerits in both approaches. I just can not decide on which approach to take. Perhaps, it depends upon how many different possible texts choices there are, but what is a good general rule of thumb?
I am thinking about this from a maintenance point of view. If you had quite a few labels on the screen and were hiding/showing them then the code would be quite messy and I would want to kill you. Also remember that ASP.Net webforms has to pump all the server side controls’ state in the form of viewstate. More viewstate means slower pages.