Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8
This is the forum to discuss the Wrox book Beginning ASP.NET 2.0 by Chris Hart, John Kauffman, David Sussman, Chris Ullman; ISBN: 9780764588501
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 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
 
Old February 12th, 2006, 11:54 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default Adding List Item Programmatically

Hi All

I wonder if you can help. Within a Page_Load event I would like to be able to add a List item to a dropdown control within my page. I thought that it would be the following syntax but doesn't like it:

Code:
MyDropdownList.DataTextField.Insert = "ALL"
MyDropdownList.DataValueField.Insert = "%"
Am I way out?

Rit
__________________
Rit
www.designandonline.co.uk
INSPIRE | CREATE | DELIVER
 
Old February 12th, 2006, 01:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, you're way out... ;)

DataTextField and DataValueField are properties that hold the name of a column / property of the data collection you're bind the control to. So, if you have a DataTable with an Id and a Description column for example, DataTextField would be "Description" and DataValueField would be "Id".

To add items, you can either add them in the markup of the page, or use the Add or the Insert method of the Items collection of the drop-down control. The latter allows you to specify the position of the item in the list.
Code:
MyDropdownList.Items.Add(new ListItem("text", "value"));
MyDropdownList.Items.Insert(4, new ListItem("text", "value"));
HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Peyote Sings by Tricky (Track 13 from the album: Angels With Dirty Faces) What's This?
 
Old February 12th, 2006, 04:51 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Blimey I was way out!

Imar, thanks for keeping me on the right track.

Rit





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Event Handling Programmatically Wallhood Excel VBA 2 August 30th, 2007 10:42 AM
adding dropdownlist item to a textbox alto ASP.NET 2.0 Basics 2 May 24th, 2007 01:14 AM
Get the first item of a List hpox ASP.NET 2.0 Basics 1 February 16th, 2007 06:04 PM
Adding datagrid programmatically at a specific pos pankaj_daga ASP.NET 1.0 and 1.1 Basics 4 October 23rd, 2003 03:10 AM





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