2

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.

Shai Cohen
  • 6,074
  • 4
  • 31
  • 54
Darthtong
  • 1,017
  • 4
  • 18
  • 30

3 Answers3

1

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.

ScottE
  • 21,530
  • 18
  • 94
  • 131
  • Exactly what I thought but there is no other control outside the Repeater with that same ID! – Darthtong May 07 '13 at 12:09
  • Is this a web app or a website? It could always be a leftover control in the design file. I duplicated this exact code without being able to reproduce your issue. – ScottE May 07 '13 at 12:11
  • It's a website, I've tried cleaning the solution but still it continues to work. – Darthtong May 07 '13 at 13:28
  • Is ...)Handles uxMyRepeater in your actual code? This shouldn't work - it should be uxMyRepeater.ItemCommand – ScottE May 07 '13 at 15:02
  • That's just a mistake when I copied the code, it is there in my proper code. See my comment on the original question for an update on what I've found. – Darthtong May 07 '13 at 15:19
0

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

Ali Umair
  • 1,386
  • 1
  • 21
  • 42
0

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.

Darthtong
  • 1,017
  • 4
  • 18
  • 30