views:

328

answers:

3

In my code I load a user control (uc) in the page_load event. The uc contains a button which sets a label text to the textbox value. (lblTest.Text = txtText.Text) . This works fine in the load event of my page. But when I set the loading of the uc in my Init event, the label isn't set. When I set breakpoints, I see the button event is fired, the text is set (in the code), but when the page is finished loading, the label is empty. How is that possible?

+1  A: 

Hello,

I dont think the control fully exists at init.

This article explains the page event cycle and may explain the issue you have: http://www.15seconds.com/issue/020102.htm

Luke Duddridge
What do you mean by 'the control doesn't fully exists at init'? Then why is the control loaded in the load event?
Martijn
+3  A: 

Check further down in the code to ensure that the control's text isn't being overwritten or set back to empty text. (Specifically, look further in Page_Init, functions that Page_Init invokes, and Page_Load and its invoked functions.)

If you're seeing that it's being set in the debugger to the value that you expect, then chances are pretty good that something else is changing it after the fact. That's going to be either your code, ViewState, or something else outside of your immediate control. Find that, and you should be able to do something about it.

Mike Hofer
I indeed had some code in my page_load which overrides the text property! So stupid..
Martijn
9 times out of 10, it's something st00pid like that. We've all done it at one time or another. :) I feel your pain.
Mike Hofer
A: 

To further elaborate a bit on the other answers.

Whenever an ASP.Net site launches, five things happen in a very specific order. Read Luke's link to find out that order and plot accordingly.

Sergio Tapia