Of course it doesn't exist - your list will contain multiple controls called
AnswerLabel
, and the compiler has no way of knowing
which AnswerLabel
control you want to change.
You need to use
FindControl
to find the specific
AnswerLabel
control within the list:
protected void Button3_Click(object sender, EventArgs e)
{
var button = (Control)sender;
var answerLabel = button.NamingContainer.FindControl("AnswerLabel");
if (answerLabel != null)
{
answerLabel.Visible = true;
button.Visible = false;
}
}