make drop down list item unselectable

后端 未结 9 514
天涯浪人
天涯浪人 2020-11-30 07:26

I have a dropdownlist which has several options for generating reports. Based on the type of account the user has certain options which should be visible but not selectable

9条回答
  •  迷失自我
    2020-11-30 07:47

    Adam Fox is absolutely correct with this snippet:

    foreach ( ListItem item in dropdownlist.Items )
    {
        if ( [item should be disabled condition] )
        {
            item.Attributes.Add( "disabled", "disabled" );
        }
    }
    

    The only caveat is that it needs to be called in the OnDataBound event, and not the OnDataBinding event (this is too early in the lifecycle).

提交回复
热议问题