开发者

Dispay specific items in dropdown list based on condition

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜