Finding parent control value in a nested datalist c#

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:DataList id="dlParent" runat="server"
OnItemDataBound="dlParent_ItemDataBound" >
<ItemTemplate>
<asp:Literal runat="server" id="litParentId_a"
Text='<%#DataBinder.Eval(Container.DataItem, "ParentId_a") %>'/>

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

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;
}
}
 
E

Eliyahu Goldin

Declare a variable in the containing page class. Set it inside
dlParent_ItemDataBound to the parent item and access it from
dlChild_ItemDataBound.
 
R

Roffers

I feel so stupid now, I can't believe I didn't think of that! Sometimes
themost obvious thing is the hardest thing to realise.

Thanks a lot.


Eliyahu said:
Declare a variable in the containing page class. Set it inside
dlParent_ItemDataBound to the parent item and access it from
dlChild_ItemDataBound.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


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:DataList id="dlParent" runat="server"
OnItemDataBound="dlParent_ItemDataBound" >
<ItemTemplate>
<asp:Literal runat="server" id="litParentId_a"
Text='<%#DataBinder.Eval(Container.DataItem, "ParentId_a") %>'/>

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

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;
}
}
 
E

Eliyahu Goldin

I won't tell you how many times I had the same feeling :)

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Roffers said:
I feel so stupid now, I can't believe I didn't think of that! Sometimes
themost obvious thing is the hardest thing to realise.

Thanks a lot.


Eliyahu said:
Declare a variable in the containing page class. Set it inside
dlParent_ItemDataBound to the parent item and access it from
dlChild_ItemDataBound.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


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:DataList id="dlParent" runat="server"
OnItemDataBound="dlParent_ItemDataBound" >
<ItemTemplate>
<asp:Literal runat="server" id="litParentId_a"
Text='<%#DataBinder.Eval(Container.DataItem, "ParentId_a") %>'/>

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

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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top