469,949 Members | 2,546 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,949 developers. It's quick & easy.

Finding parent control value in a nested datalist c#

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).Va lue = 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;
}
}

Sep 16 '06 #1
3 7843
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]
<mr*****@hotmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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).Va lue = 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;
}
}

Sep 17 '06 #2
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 Goldin wrote:
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]
<mr*****@hotmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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).Va lue = 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;
}
}
Sep 17 '06 #3
I won't tell you how many times I had the same feeling :)

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Roffers" <mr*****@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>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 Goldin wrote:
>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]
<mr*****@hotmail.comwrote in message
news:11**********************@e3g2000cwe.googlegr oups.com...
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).Va lue = 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;
}
}

Sep 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mary Kerrigan | last post: by
reply views Thread by Dave | last post: by
3 posts views Thread by Derek | last post: by
1 post views Thread by Adam Knight | last post: by
1 post views Thread by schapopa | last post: by
reply views Thread by =?Utf-8?B?Y2luZHk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.