How can server control inside a template be sensitive to a data context?

Let's say that the X control has a template named RowTemplate.

So, the X markup will look like this:

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

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

Therefore, I do not know how to use the object identifier (Eval ("Id")) in Y.

+3
source share
1 answer

ItemDataBound ( foo: X), . DataList, , , .

- :

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

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

. DataRow, .

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"];
}
+1

Source: https://habr.com/ru/post/1729989/


All Articles