M
mroffey
Hi everyone,
I've been having this problem and I've searched high and low, but I
can't seem to find the answer to what is probably a very simple
solution.
In a nutshell, I have a nested datalist, Parent and Child. In the child
datalist, I need to get 1 of the values from the parent datalist. I've
included the dataaccess code below, only for completeness.
I hope someone out there can help me.
Thanks in advance.
Mark
- - - - - - - - - - - - - - - - - - - - - - - - - -
<asp
ataList id="dlParent" runat="server"
OnItemDataBound="dlParent_ItemDataBound" >
<ItemTemplate>
<asp:Literal runat="server" id="litParentId_a"
Text='<%#DataBinder.Eval(Container.DataItem, "ParentId_a") %>'/>
<asp
ataList id="dlChild" runat="server"
OnItemDataBound="dlChild_ItemDataBound">
<ItemTemplate>
<asp:Literal runat="server" id="litChildId_a"/>
</ItemTemplate>
</asp
ataList>
</ItemTemplate>
</asp
ataList>
</asp
ataList>
public void dlParent_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
SqlConnection objConn = new SqlConnection(ConnectionString);
SqlCommand objCom = null;
SqlDataAdapter objDa = new SqlDataAdapter();
DataSet objDs = new DataSet();
switch( e.Item.ItemType )
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
objCom = new SqlCommand("exec_my_sp", objConn);
objCom.Connection = objConn;
objCom.CommandType = CommandType.StoredProcedure;
objCom.Parameters.Add("@my_id",SqlDbType.Int,4).Value = 1;
objDa.SelectCommand = objCom;
objDs = new DataSet();
objDa.Fill (objDS);
( (DataList) e.Item.FindControl("dlChild") ).DataSource =
objDs.Tables[0].DefaultView;
( (DataList) e.Item.FindControl("dlChild") ).DataBind();
}
}
public void dlChild_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
switch( e.Item.ItemType )
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
//THIS IS WHERE I NEED TO RETRIEVE THE VALUE HELD IN litParentId_a
//OR DIRECTLY ACCESS THE DATAITEM ParentId_a FROM THE PARENT
DATALIST
Literal litChildId_a = (Literal)e.Item.FindControl("litChildId_a");
litChildId_a.Text = litParentId_a.Text
break;
}
}
I've been having this problem and I've searched high and low, but I
can't seem to find the answer to what is probably a very simple
solution.
In a nutshell, I have a nested datalist, Parent and Child. In the child
datalist, I need to get 1 of the values from the parent datalist. I've
included the dataaccess code below, only for completeness.
I hope someone out there can help me.
Thanks in advance.
Mark
- - - - - - - - - - - - - - - - - - - - - - - - - -
<asp

OnItemDataBound="dlParent_ItemDataBound" >
<ItemTemplate>
<asp:Literal runat="server" id="litParentId_a"
Text='<%#DataBinder.Eval(Container.DataItem, "ParentId_a") %>'/>
<asp

OnItemDataBound="dlChild_ItemDataBound">
<ItemTemplate>
<asp:Literal runat="server" id="litChildId_a"/>
</ItemTemplate>
</asp

</ItemTemplate>
</asp

</asp

public void dlParent_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
SqlConnection objConn = new SqlConnection(ConnectionString);
SqlCommand objCom = null;
SqlDataAdapter objDa = new SqlDataAdapter();
DataSet objDs = new DataSet();
switch( e.Item.ItemType )
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
objCom = new SqlCommand("exec_my_sp", objConn);
objCom.Connection = objConn;
objCom.CommandType = CommandType.StoredProcedure;
objCom.Parameters.Add("@my_id",SqlDbType.Int,4).Value = 1;
objDa.SelectCommand = objCom;
objDs = new DataSet();
objDa.Fill (objDS);
( (DataList) e.Item.FindControl("dlChild") ).DataSource =
objDs.Tables[0].DefaultView;
( (DataList) e.Item.FindControl("dlChild") ).DataBind();
}
}
public void dlChild_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
switch( e.Item.ItemType )
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
//THIS IS WHERE I NEED TO RETRIEVE THE VALUE HELD IN litParentId_a
//OR DIRECTLY ACCESS THE DATAITEM ParentId_a FROM THE PARENT
DATALIST
Literal litChildId_a = (Literal)e.Item.FindControl("litChildId_a");
litChildId_a.Text = litParentId_a.Text
break;
}
}