Using ASP.NET 4.0

Bit of a strange one here, my code works but I don't know why!

So I have some HTML like so:

<asp:Repeater runat="server" ID="uxMyRepeater" ClientIDMode="Predictable">
    <ItemTemplate>
        <asp:Button runat="server" Text="Submit" />
        <asp:HiddenField runat="server" ID="uxIsVisibleHiddenField" Value="0" />
    </ItemTemplate>
</asp:Repeater>

And the back end:

Protected Sub uxMyRepeater_ItemCommand(source As Object, e As RepeaterCommandEventArgs) Handles uxMyRepeater.ItemCommand
    uxIsVisibleHiddenField.Value = "1"
End Sub

So for some reason this works, usually I would expect to have to declare uxIsVisibleHiddenField in uxMyRepeater_ItemCommand like so:

Dim uxIsVisibleHiddenField As HiddenField = DirectCast(e.Item.FindControl("uxIsVisibleHiddenField"), HiddenField)

But in this particular case it works without the declarative statement. Can anyone shed any light on why it would do this?

Please note this is sample code only, not my actual code.

EDIT

Forgot to mention there is an UpdatePanel around each RepeaterItem, removing this causes Visual Studio to give me an error that'd I'd expect: 'uxIsVisibleHiddenField' is not declared. It may be inaccessible due to its protection level.

有帮助吗?

解决方案 3

After a lot of debugging the only thing I can say is that when I have an UpdatePanel inside the Repeaters ItemTemplate I don't need to declare the controls inside the ItemTemplate when accessing them in the DataBind event, very strange. Taking out the UpdatePanel causes complier errors so the UpdatePanel must be doing some auto hook-up between the Repeater and the controls.

Thanks for all your suggestions.

其他提示

This could only happen if you have a control with the same ID that sits outside of the repeater. You won't have ID clashes because the repeater is a naming container.

Do you have any AlternatingItemTemplate ? It might be declared in that particular area and remained unnoticed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top