Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old July 10th, 2003, 11:13 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
  #2 (permalink)  
Old July 10th, 2003, 03:10 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
  #3 (permalink)  
Old July 10th, 2003, 03:16 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
  #4 (permalink)  
Old July 10th, 2003, 03:28 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
  #5 (permalink)  
Old July 10th, 2003, 03:34 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #6 (permalink)  
Old July 10th, 2003, 03:48 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
  #7 (permalink)  
Old July 10th, 2003, 04:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Wien, Wien, Austria.
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

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
  #8 (permalink)  
Old August 30th, 2007, 10:31 AM
Registered User
 
Join Date: Aug 2007
Location: Orlando, FL, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();



Similar Threads
Thread Thread Starter Forum Replies Last Post
Datalist inside datalist amit.jagtap ASP.NET 2.0 Professional 1 September 4th, 2007 05:03 AM
check n uncheck items in checkboxlist in datalist moorejkrkumar ASP.NET 1.0 and 1.1 Basics 1 April 17th, 2007 01:39 PM
Clear Variables dmerrall Javascript How-To 2 June 20th, 2005 08:27 AM
displaying 6 items only having 20 items Lakshmi KS VB Components 1 February 17th, 2004 10:34 AM
clear items in asp.datalist jtyson Classic ASP Basics 0 July 10th, 2003 12:19 PM





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