Friday, March 03, 2006

Setting asp:Label texts too early

Just discovered that if you have method calls to the code-behind (typically a protected method in the codebehind called between <% %> tags) You cannot alter The text property on labels in that method.

This probably extends to manipulating any property on any webcontrol defined in the code-front. I guess it's just too early.

when stepping through the code The label had its original value (which obviously would have been read/instantiated in the code-front), I replaced it with a new value and when the page rendered it had turned back to the original value.

I guess this means that the code-front is read twice, before method calls in it are handled, and after. Is it just me that find this weird?

The fix to my problem was easy, I just placed the method-call in Page_Load, so no biggie, but I was stumped on it for a while

Edit: OK, so I might have missed the obvious, but of course the ViewState is copied back.. and overwrites my changes. Since ViewState is copied just before Page_Load, The earliest you can write to weeb-controls is page_load

No comments: