views:

63

answers:

2

I have a user control embedded in a web part. It has the following snippets of code:

protected TextBox searchTextBox;
protected Button goButton;
protected Button nextButton;
protected Repeater pushpins;
protected Label currentPageLabel;

protected void goButton_Click(object sender, EventArgs e)
{
 // Do stuff
}

<asp:TextBox ID="searchTextBox" runat="server" />
<asp:Button ID="goButton" runat="server" Text="Go" OnClick="goButton_Click" />
<asp:Repeater ID="pushpins" runat="server" EnableViewState="false">
 <ItemTemplate>Blah</ItemTemplate>
</asp:Repeater>
<asp:Label ID="currentPageLabel" runat="server" />
<asp:Button ID="nextButton" runat="server" Text=" >> " OnClick="nextButton_Click" />

(There is no OnLoad or CreateChildControls method.)

If I place a breakpoint on the first line of goButton_Click, I find:

  • searchTextBox: initialised
  • goButton: initialised
  • nextButton: NULL
  • pushpins: initialised
  • currentPageLabel: NULL

Why are some controls initialised and others not? How do I get around this if I'd like to update the Text property on currentPageLabel?

Update:

I've placed breakpoints all the way through the page life cycle and found that nextButton and currentPageLabel are never initialised. The breakpoints were placed at OnLoad, CreateChildControls, goButton_Click, OnPreRender, Render and OnUnload.

A: 

Is it possible you've inadvertently got local variables named currentPageLabel and/or nextButton declared somewhere? The compiler will happily allow this...

This is especially common in a move from ASP.NET 1.1 to 2.0 or higher, since the way the designer defined these controls changed.

Bryan
A: 

The problem was that all of the controls that weren't being initialised were inside a HeaderTemplate within a Repeater control. (To save space I didn't paste all of my code into the question, dang it.)

I moved them out and the problem is resolved. Obviously controls in the HeaderTemplate are instantiated by the Repeater and do not follow the normal page life cycle.

Alex Angas