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
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
senderargument, because that’s always the source control: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.vbfile:So why using
FindControlif 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
FindControlin aContentPageof aMasterPage. This is a special case, you need to get the reference to theContentPlaceholderfirst. Then you can useFindControlfor yourButton:But again, this is pointless since you have the reference in the page directly.