Reading contents of DataList problem

D

David

Hi,

This one appears a little strange to me. Here is the scenario.

I load a datalist the first time a page is run. The page has a !IsPostback
and the datalist gets loaded at this point.

I have a button on the page, where I want to read the contents of the
datalist. As an example that I have found on MS...

if (MyDataList.Items.Count > 0)

{

Response.Write("The Items collection contains: " + MyDataList.Items.Count +
"<br>");

foreach(DataListItem item in MyDataList.Items)

{


Response.Write(((Label)item.Controls[0]).Text + "<br>");

}

}

Now, here is the puzzling part.

The datalist reports 7 items in the items.count. This is absolutely correct,
BUT, I cannot read any of the items.

Initially, I was trying to read it as data from the datalist. As this was
not working, I have tried setting up a hidden label with the text. (I have
made the label visible to see what is happening)

Neither of them appear to contain anything, but the return count is 7 items.

The way I am doing it is practically the same way that MS are doing it...

http://msdn2.microsoft.com/en-us/system.web.ui.webcontrols.datalist.items.aspx

The only difference is that the data in my itemTemplate of the datalist is
inside a label control. (Incidentally, I have also tried doing
item.FindControl("LabelName") to get something out)

Any help would be appreciated.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
D

David

I guess this is too difficult to answer.

In order to get me closer, I have been experimenting. I have found some
further detail that may lead someone to help me.

From the MS link in my original posting, I have written a test page that I
actually take the same data from my database. This works as expected.

I then modify the listing to put a label around the content in the datalist
(instead of it being a bound literal).

I then try reading the label with
((label)item.findcontrol("labelname")).text which then fails.

Why does having a label cause it to fail?

Any help on this would be appreciated.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 

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

Top