0

I created a simple page with one button, then on the click event have it use FindControl, to get a reference to itself. But..... FindControl is returning nothing. code

Protected Sub EntryDoor1_Click(sender As Object, e As System.EventArgs) Handles EntryDoor1.Click
    Dim control = FindControl("EntryDoor1")
    control.Visible = False
End Sub
Jacob
  • 77,566
  • 24
  • 149
  • 228
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

1 Answers1

6

Because you've said that you want "a reference to itself", i assume that you want a reference to the button that has caused the click-event.

The easiest is to use the sender argument, because that's always the source control:

Dim button = DirectCast(sender, Button)

But when the button is on top of the page(as in this case), the reference to the control is automatically created in the partial designer.vb file:

 EntryDoor1.Visible = False

So why using FindControl if you have a direct reference anyway?!

Edit:

Just for the sake of completeness. The behaviour you're describing can only have one reason: You're trying to use FindControl in a ContentPage of a MasterPage. This is a special case, you need to get the reference to the ContentPlaceholder first. Then you can use FindControl for your Button:

Dim button = DirectCast(Page.Master.FindControl("ContentPlaceHolder1").FindControl("EntryDoor1"), Button)

But again, this is pointless since you have the reference in the page directly.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939