views:

83

answers:

3

I have a dropdown list with four options like:

  • New
  • Reviewed
  • To be Reviewed
  • Presented

I need to display only specific items in the dropdown list based on some conditions. I mean sometimes with only 2 items

  • New
  • Review

Sometimes with 3 items

  • New
  • Review
  • To be Reviewed

and sometimes all items. How can I do this? I am using C#.

+1  A: 
if (condition)
{
     ddlList.Items.Add(new ListItem("Text", "Value"));
}
meep
I have already bound the drop downlist using datasource. I need to hide the unnecessary items.
ANP
A: 

You need to change the DataSource Property of the Dropdown.

based upon condition , you need to filter data from your source and then rebind it.

saurabh
A: 

In the DataBound event of the dropdown, you can loop through the Items collection and remove any items that need to be filtered. The only real trick is to loop backwards through the collection, so that you can remove items without messing up your iterator location.

Private Sub MyDropDownList_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyDropDownList.DataBound
    For x As Integer = MyDropDownList.Items.Count - 1 To 0 Step -1
        If RemoveToBeReviewed()
            If MyDropDownList.Items(x).Text = "To Be Reviewed" Then
                MyDropDownList.Items.RemoveAt(x)
            End If
        End If
    Next
End Sub
Jason Berkan