Trying to access a Repeater's HeaderTemplate controls from event fired in an ItemTemplate control
-
I've got an ASP .NET webpage with the following structure:
<asp:DataGrid ID="aGrid">
<Columns>
asp:TemplateColumn
<ItemTemplate>
<asp:Repeater ID="aRepeater">
<HeaderTemplate>
<asp:Label ID="aLabel" Text='<%# DataBinder.Eval(Container.DataItem, "SomeColumn") %>'></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="aTextBox" AutoPostBack="true" OnTextChanged="SomeEvent"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>The problem I'm having is that I would like to access the data that is stored in the label
aLabel
in the OnTextChanged event. What I'm doing to achieve this purpose is:void SomeEvent(object sender, EventArgs e)
{
//Bind aRepeater to a certain data source in the ItemDataBound event of aGrid//Get reference to aRepeater //Parent gets you the RepeaterItem //Parent.Parent gets you the Repeater Repeater r = (sender as TextBox).Parent.Parent; //Assert that r.DataSource != null
}
My assertion, however, fails, and even if I get
aLabel.Text
, it ends up being an empty string, sincer.DataSource
is null (despite the fact that in the ItemDataBound event of aGrid, I already bound the repeater to a data source). After some debugging, I noticed thatRepeater r
is not the same repeater as the one that I bound in the ItemDataBound event of aGrid, but how could that be? In the ItemDataBound event of aGrid, I do the following...//e is DataGridItemEventArgs object in ItemDataBound event e.Item.FindControl("aRepeater") as Repeater;
...which I assume should be equivalent to...(sender as TextBox).Parent.Parent;
Sorry for the long post.