I'm in a web page and I have hierarchical data so I'm using 2 nested
DataList controls.
DataList1 is using a data source with rows of [Name], [ID], [Status] and
[MoreData] where MoreData is a list.
DataList1 has an ItemTemplate with Literal controls for Name, ID and Status
and a DataList control for MoreData.
DataList2 is using MoreData where fields are [Protocol], [Domain] and
[Port].
DataList2 has an ItemTemplate with a HypeLink control.
I want to set the hyperlink's .Enabled property dependent upon the value in
Status, so when I'm binding the row in DataList 2 I need to get a handle on
the current row in DataList1.
This should be simple, right?
I searched high and low for a way to get a handle on the parent, and the
nearest I could find was a suggestion dating from 2006 where you set a
property on the page to hold a copy of the parent object. In the
OnItemDataBound event of DataList1 you set the property tot he current row,
then in the OnItemDataBound event of DataList2 you can reference the
property and all is good.
Wrong!
For some wierd reason my page is rendered out of sequence. It took me a
while to pin it down because nothing seemed to fail anywhere, but i added an
extra control to my child DataList to reveal the parent data and found what
is happening, but not why or how to fix it. Best I can do is illustrate like
this...
Name=Parent1, ID=P1, Status=Active
Protocol=http, Domain=mydomain.com, Port=80, ParentStatus=undefined,
ParentName=undefined
Protocol=http, Domain=www.mydomain.com, Port=80, ParentStatus=undefined,
ParentName=undefined
Name=Parent2, ID=P2, Status=Active
Protocol=http, Domain=otherdomain.com, Port=80, ParentStatus=Active,
ParentName=Parent1
Protocol=http, Domain=www.otherdomain.com, Port=80, ParentStatus=Active,
ParentName=Parent1
Name=Parent3, ID=P3, Status=Disabled
Protocol=http, Domain=thirdurl.com, Port=80, ParentStatus=Active,
ParentName=Parent2
Protocol=http, Domain=www.thirdurl.com, Port=80, ParentStatus=Active,
ParentName=Parent2
Name=Parent4, ID=P4, Status=Disabled
Protocol=http, Domain=fourthdomain.com, Port=80, ParentStatus=Disabled,
ParentName=Parent3
Protocol=http, Domain=www.fourthdomain.com, Port=80,
ParentStatus=Disabled, ParentName=Parent3
In the codebehind I have these lines...
public Website currentSite;
protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
(e.Item.ItemType == ListItemType.Item))
{
currentSite = (Website)e.Item.DataItem;
Label l = (Label)e.Item.FindControl("StatusLabel");
if (currentSite.Status == WebsiteStatus.Started)
{
l.Text = "Started";
}
else
{
l.Text = "Not started";
}
}
}
protected void DataList2_ItemDataBound(object sender,
DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
(e.Item.ItemType == ListItemType.Item))
{
HyperLink h = (HyperLink)e.Item.FindControl("hLink");
h.Enabled = (currentSite.Status == WebsiteStatus.Started);
// extra debugging display labels
Label dl = (Label)e.Item.FindControl("DebugLabel");
if (currentSite.Status == WebsiteStatus.Started)
{
dl.Text = "ParentStatus=Started";
}
else
{
dl.Text = "ParentStatus=Not started";
}
dl.text += ", ParentName=" + currentSite.Name
// end of extra debugging display label code
}
}
I hope you can see from this that even though the first thing that happend
when DataList1 gets bound is that the global currentSite gets set to the
current site,
DataList2 seems to be getting bound *before* the currentSite variable is
set, but *somehow* it uses the correct list for the current site.
I'm soooo confused.
Is there a way I can control the timing of these binding events so that my
child get the proper handle on its parent?
Or is there a better way altogether of achiving my target?