 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 10th, 2003, 11:13 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 10th, 2003, 03:10 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

July 10th, 2003, 03:16 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

July 10th, 2003, 03:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

July 10th, 2003, 03:34 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 10th, 2003, 03:48 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

July 10th, 2003, 04:49 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

August 30th, 2007, 10:31 AM
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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();
|
|
 |