p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Basics (http://p2p.wrox.com/forumdisplay.php?f=60)
-   -   clear items in datalist (http://p2p.wrox.com/showthread.php?t=1503)

jtyson July 10th, 2003 11:13 AM

clear items in datalist
 

Hi,

Can someone tell me how to clear all items from a previously populated datalist? Something similar to clearSelection() with a dropdownlist.

Thank you,

John

Imar July 10th, 2003 03:10 PM

Hi there,

I think you are referring to the Clear method of the controls's Item collection:

Code:

MyDataList.Items.Clear()
HtH

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.

jtyson July 10th, 2003 03:16 PM

Tried that and got the following error:

'Clear' is not a member of 'System.Web.UI.WebControls.DataListItemCollection

Thank you for your response. Do you have any further thoughts on this?

Imar July 10th, 2003 03:28 PM

Right, sorry. I misunderstood your question and mixed up the DataList with a DropDown control.

In that case, I don't really know the answer to your question.

You may want to loop through the Items collection, setting each Item's Visible property to False, or you may try to Dispose() an Item, although I am not sure that would work.

In what scenario do you need this? Isn't it easier to rebind your list to the original datasource where you have removed the item??

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.

jtyson July 10th, 2003 03:34 PM

Unfortunately this page is fairly complicated to repost in. Basically I have two dropdown lists with a button that when clicked populates a datalist based on the selections in the dropdownlists. The two dropdownlists are related, so there is a change event for when the first dropdownlist selection is changed. I wanted to place some code in the change event that would clear the datalist if the user changed the selection. I guess instead I could always hide the datalist and then unhide it again when the button is clicked.

I also found this in reference to your previous post, and it seems somewhere I was able to get this working but can't find the example right now:

Use the Items collection to programmatically control the items in the DataList control. The Items collection does not provide any methods to add or remove items to the collection. However, you can control the contents of an item by providing a handler for the ItemCreated event.

Thank you,

John

Imar July 10th, 2003 03:48 PM

How about killing the DataList altogether, something like this:

Code:

MyDataList = New DataList()
SetupDataList(MyDataList)

In the SetupDataList method, you can add formatting and other behavior you might need.

The ItemCreated event allows you to customize the creation of an item. You can apply custom formatting, hide items etc. Check out the MSDN docs for the ItemCreated event for more info.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.

NotNowJohn July 10th, 2003 04:49 PM

If the datalist is bind to DataSet, you can bind it again (in the Clear List button's procedure) to new DataSet created by the Clone() method of the old DataSet. In that way the bind expressions u use in DataList's ItemTemplate are still valid.

HTH.
NNJ

...but the Soon is eclipsed by the Moon

ChrisShaw August 30th, 2007 10:31 AM

I realize this is an old thread but for anyone else googling the topic, the way to clear your datalist is this:

MyDataList.DataSource = null;
MyDataList.DataBind();



All times are GMT -4. The time now is 04:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.