views:

39

answers:

1

Let's say a control X has a template called RowTemplate.

So X's markup would be like:

<foo:X>
    <RowTemplate>
        <foo:Y>...</foo:Y>
    </RowTemplate>
</foo:X>

My question is: How can the Y control be sensitive to the data context? I know I can use template inline tags to get access to the data context: <%# Eval("Id") %>, but I cannot pass this information to Y because template inline tags are not allowed in server controls.

So I don't know how I could use the Object's Id (Eval("Id")) in Y.

+1  A: 

By adding a handler to the ItemDataBound event (or some other similar event on your foo:X control), you can access controls in your row template. My example code is from a DataList, so your event handlers will probably be different.

In the code behind - wire up the event handler:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    foo.ItemDataBound += new DataListItemEventHandler(foo_ItemDataBound);
}

Then in the event handler, access the controls in your row. Your data might not be a DataRow, so change that as needed.

void foo_ItemDataBound(object sender, DataListItemEventArgs e)
{
    Control fooY = (e.Item.FindControl("foo:Y") as Control); //Replace foo:Y with the ID for foo:Y
    DataRow data = e.Item.DataItem as DataRow;
    fooY.SomeProperty = data["id"];
}
s_hewitt
@hewitt: I was searching for something more straight forward but it seems your solution is the only one available (or there's no other better) so I'm going to implement this. As the foo:X control is mine I'm gonna expose this event and fire it after I bind each item. Thank you for you time.
Ciwee