For Programmers: Free Programming Magazines  


Home > Archive > C# > May 2005 > Removing items from A dropdown List Please help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Removing items from A dropdown List Please help
frawls@gmail.com

2005-05-09, 3:59 pm

Hi,


This is concerned with System.Web.UI.WebControls.Drop=ADDownList


I am having problems creating a method which will remove list items
from a preloaded dropdownlist. This dropdown is loaded when the page
loads and the items are pulled from a database.


I want to create a method which is called on the page load and will
remove certain items from the dropdown list. I want the method to loop
through each list item and remove it if its value =3D "Not Given"


The reason i am dong this is because i have to load all the items in
first and then use a different method to remove the undesired ones. I
do not want the user to be able to select or even see the "Not Given"
Option


My dropdown list is called ddlOccupationStatus
My method is called ClipDropDowns();


I cannot get the correct logic in place to loop through the list and
the remove the undesired items, can anyone help?


Any answers or help will be greatly appreciated....


I have tried the follwing methods but to no avail:


private void ClipDropDowns()
{
if (ddlOccupationStatus.SelectedV=ADalue.Equals("Not Asked"))
{
ddlOccupationStatus.SelectedVa=ADlue.Remove("Not Asked);
}



}


I have also tried:

private void ClipDropDowns()
{
for(int i=3D0; i < 20; i++)
{
if (ddlOccupationStatus.Items(i) =3D=3D "Not Asked")
{
ddlOccupationStatus.Items.Remo=ADve(i,1);=20
}=20


}
}

Angrez Singh

2005-05-09, 3:59 pm

Hi,

Just try this out :
private void RemoveValue()
{
for(int i=0; i<drpList.Items.Count; i++)
{
if(drpList.Items[i].Value == "true")
{
drpList.Items.RemoveAt(i);
// Here i is decremented because when you remove
the value from the
// the list the length is automatically decreased by
one. So the next item
// will be a index i and if we increment it we skip
one value.
i--;
}
}
}


HTH,
Regards,
Angrez

Frawls

2005-05-10, 8:57 am

Hi Angrez

Thank you for you reply, thats great. The solution you gave me worked
perfectly!! Thank you so much, i really appreciate your help, i will be
implementing your solution all around my application....

Frawls

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com