Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1.1 DataList nested in a datalist

Status
Not open for further replies.

sandylou

Programmer
Jan 18, 2002
147
US
I have a datalist
Code:
<asp:datalist id="dlstEmps" runat="server>
<HeaderTemplate>
Some Code...
</HeaderTemplate>
<ItemTemplate>
Some Code..

[bold]<asp:datalist id="datalist2" runat="server>
<ItemTemplate>some code..</ItemTemplate>
</asp:datalist>[/bold]

</ItemTemplate>
<FooterTemplate>
Some Code...
</FooterTemplate>
</asp:datalist>

is is possible in this to put another datalist? I have tried adding a datalist in the but get: "Object reference not set to an instance of an object." when I try and load the data in the second datalist.
 
this is works 2.0+ the problem is probably your code, not the markup. post the relevant code behind.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I did step through and it says it has rows, but when I try and find out anything about the dlstdatalist2 the object can not be found. It errors when i try to set the datalist datasource to my data reader.

I am able to successfully populate the first datalist.

Code:
 Dim cmdData As SqlCommand
        Dim dtrDataInfo As SqlDataReader
        Dim conDatabase As SqlConnection
        Dim strSelEmp As String
        strSelEmp = Session("txtSelEmp")

        conDatabase = New SqlConnection(Application("txtDBConnection"))
        cmdData = New SqlCommand("spgEmployeeContacts", conDatabase)
        cmdData.CommandType = CommandType.StoredProcedure

        cmdData.Parameters.Add("@chrSSNEmp", strSelEmp)

        conDatabase.Open()

        dtrDataInfo = cmdData.ExecuteReader()

        dlstDataList2.DataSource = dtrDataInfo

        dlstDataList2.DataBind()

        dtrDataInfo.Close()
        conDatabase.Close()
 
You are not posting all of the relevant code so it is hard to determine what you are doing and what is failing.

What you have to do is first bind the Outer datalist.
Then, in the itemdatabound event of that datalist, use FindControl() to get a reference to the Inner datalist.
Once you do that, you can then bind the Inner datalist.
 
dlstDataList2 only exists within dlstEmps so you need to put the binding code within the dlstEmps.ItemDataBound event handler. the code will look something like this
Code:
<asp:datalist id="dlstEmps" runat="server" ItemDataBound="load_data_list_2">
...
Code:
DataTable data;

protected override void OnLoad(EventArgs e)
{
   load_data_from_your_sql_command();
}

private void load_data_from_your_sql_command()
{
   using(SqlConnection cnn = new SqlConnection(...))
   using(SqlCommand cmd = new SqlCommand("proc", cnn))
   {
      cnn.Open();
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.AddParameterWithValue("key", value);
      data.Load(cmd.ExecuteReader());
   } // auto close/dispose
}

protected void load_data_list_2(object sender, DataListItemDataBoundEventArgs e)
{
   if(e.ItemIndex == -1) return;

   DataList ctrl = e.Item.FindControl("dlstDataList2") as DataList;
   ctrl.DataSource = data;
   dtrl.DataBind();
}
1st according to your stored proc it's a one time call, so we load this during the page load event and store in a private field.
2nd we make sure the current item is not a header, footer or seperator.
3rd bind data.


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
thanks jason, i think as in similar responses to you, i am missing something... i moved from traditional asp to asp.net using vb and i think you are using c#. I think i need to add the ItemDataBound event to my 'parent' datalist and then I should be able to find the control. I guess i am not sure about the steps in the process..

first onload event of the page fires the load data from sql.. then the list has an itemdatabound which will fire when it is loaded and pull the data from the reader that is stored in the data object and bind to the inner datalist. however, i just don't understand where the data.Load(cmd.ExecuteReader()); comes from. i do not have that option available...

i think this is the same problem i couldn't figure out before with another project I tried to work out, but ended up re-coding it completely different. I am not sure if you know of another means (class to take or book to read) or private tutor!! :)

I will haven't stepped through with the new code, since i am coding in .net so i will and then send error..
 
i think you are using c#
yes, but the principles are the same.

I think i need to add the ItemDataBound event to my 'parent' datalist and then I should be able to find the control.
yes. I wasn't clear about that.

i just don't understand where the data.Load(cmd.ExecuteReader()); comes from.
Load(data reader); is member on DataTable (at least in 2.0)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
it makes sense what you are saying, but can not find the control. I have tried this:

Code:
<asp:DataList ID="dlstEmpInfo" Runat="server" OnItemDataBound="dlstEmpInfo_ItemDataBound" >	
	<HeaderTemplate>
	<TABLE align="center" cellSpacing="5" cellPadding="0" bgColor="#9cbdde" border="2">
	</HeaderTemplate>
	<ItemTemplate>	
	<tr><td>blah blah, all of the employee detail</td>></tr>
		<asp:datalist ID="dlstEmployeeContacts" Runat="server">										<ItemTemplate><tr><td>employee contacts</td></tr>
		</ItemTemplate>
		</asp:datalist>
	</ItemTemplate>
	<FooterTemplate>
	<tr><td height="300">&nbsp;</td></tr>
	</TABLE>
	</FooterTemplate>
</asp:DataList>

Sub dlstEmpInfo_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlstEmpInfo.ItemDataBound
        Dim dstContactInfo As DataList = CType(Me.FindControl("dlstEmployeeContact"), DataList)
        Dim cmdContactInfo As SqlCommand
        Dim dtrContactInfo As SqlDataReader
        Dim conDatabase As SqlConnection
        Dim strSelEmp As String
        
        strSelEmp = Session("txtSelEmp")

        conDatabase = New SqlConnection(Application("txtDBConnection"))
        cmdContactInfo = New SqlCommand("spgEmployeeContact", conDatabase)
        cmdContactInfo.CommandType = CommandType.StoredProcedure

        cmdContactInfo.Parameters.Add("@chrSSNEmp", strSelEmp)

        conDatabase.Open()

        dtrContactInfo = cmdContactInfo.ExecuteReader()

        dstContactInfo.DataSource = dtrContactInfo 
        dstContactInfo.DataBind()
	
	dtrContactInfo.Close()
        conDatabase.Close()


    End Sub

and various iterations, but the object can not be found. i can find the first datalist, but not the one nested.
 
Code:
Dim dstContactInfo As DataList = CType([blue][s]Me[/s][/blue].FindControl("dlstEmployeeContact"), DataList)
Me referes to the page, so it will not find the nested datalist

Try
Code:
Dim dstContactInfo As DataList = CType([b][red]e.item[/red][/b].FindControl("dlstEmployeeContact"), DataList)
 
you also need to include a null check. itemdatabound applies to all types, header, footer, separator, alternate item and item.

on you convert the control to a DataList you need to check if it is null and escape. you could also check the item row index. if it's -1 exit.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
how would I check for null and if the row index = -1 in 1.1?
 
e.Item.ItemIndex == -1;
or
DataList list = FindControl("name of control") as DataList;
if(list == null) return;


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I am not sure how I would check for null in vb. basically i am checking to see if the datalist is null? why would it be?

i still am unable to find the datalist.. i says the referenced object has a value of nothing
 
i think i figured it out.. i looked at all of the values of e and realized that it was the e.Item.ItemIndex = -1

Thank you!
 
There are many posts in the forum as well as online on how to do this:
Code:
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim dstContactInfo As DataList = CType(e.Item.FindControl("dlstEmployeeContact"), DataList)
        End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top