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#.
if (condition)
{
ddlList.Items.Add(new ListItem("Text", "Value"));
}
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
You can make the item visible as follows, assuming tstr
is your menu item:
tstr.DropDownItems[i].Visible = false;
where i
is the index of your item.
精彩评论