Best way to check if a drop down list contains a value?

前端 未结 10 2226
北海茫月
北海茫月 2020-12-25 09:44

When the user navigates to a new page, this ddl\'s selected index is determined by a cookie, but if the ddl doesn\'t contain that cookie\'s value, then I\'d like it to be se

10条回答
  •  长情又很酷
    2020-12-25 10:12

    Sometimes the value needs to be trimmed of whitespace or it won't be matched, in such case this additional step can be used (source):

    if(((DropDownList) myControl1).Items.Cast().Select(i => i.Value.Trim() == ctrl.value.Trim()).FirstOrDefault() != null){}
    

提交回复
热议问题