views:

251

answers:

3

I have an ASP page with the following repeater:

<asp:Repeater runat="server" ID="RegionRepeater"
    DataSourceID="SqlDataSourceRegions" EnableViewState="true">
    <ItemTemplate>
        <tr>
            <td valign="top">
                <b><%#Eval("description")%></b>
                <asp:HiddenField runat="server" ID="RegionID"
                    Value='<%#Eval("region_id")%>'/>
            </td>
            <td>
                <asp:FileUpload ID="FileUpload" runat="server" Width="368px" />
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

(The repeater is inside a Wizard, inside a content pane).

The code behind is connected to the

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)

event. There are two items on the screen (two rows inside the table). However, when the code tries to read those items, the Items collection is empty!

foreach(RepeaterItem region in RegionRepeater.Items)
{
    // Never runs - the RegionRepeater.Items.Count = 0
    FileUpload fileUpload = (FileUpload) region.FindControl("FileUpload");
    String regionID = ((HiddenField)region.FindControl("RegionID")).Value;
    ...

Why is the collection empty, when there are controls drawn on the screen?

Thanks a lot for any help; this is starting to drive me nuts.

(BTW: I tried adding/removing the EnableViewState="true" tag)

+2  A: 

Have you made sure the repeater has been rebound on the postback?

The ASP.NET Page Life Cycle means that on a postback, you will need to rebind controls like the repeater in order for event handlers to be able to see the data.

Oded
Doh! Thank you. Very much! (Other answers are all correct - thanks for your time. This one was first, so I marked it as accepted)
Ryan
A: 

Maybe you do the databinding every time you load the page, do you check that there is no postbacl before you do it ?

remi bourgarel
A: 

Hey,

I believe Items is only populated when bound. So you have to rebind your data on every page load. This is due to the statless environment of the web; it doesn't remember the data source, but loads the created controls from viewstate that were bound to it.

So on subsequent requests, it loads the control hierarchy but knows nothing about the data source that created the UI unless you rebind again.

HTH.

Brian