The problem here is that lnkBookNow.NamingContainer
is rptRooms
. This control obviously does not contain hdnHotelCode
.
I think you should be able to do this with:
protected void lnkBookNow_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
var item = (RepeaterItem)btn.NamingContainer.NamingContainer.NamingContainer;
HiddenField hdnHotelCode = item.FindControl("hdnHotelCode") as HiddenField;
}
btn.NamingContainer
is a RepeaterItem
in rptRooms
. The NamingContainer
of that is the Repeater
itself. Finally, the NamingContainer
of rptRooms
is the RepeaterItem
of rptHotels
, in which you want to find your HiddenField
.
Note my use of the as
keyword instead of an explicit cast - this will protect you from NullReferenceExceptions if FindControl
returns null. Of course, you should explicitly check that hdnHotelCode
isn't null before you try to access it.