开发者

How to get the last option value from a DropDownList?

开发者 https://www.devze.com 2022-12-31 11:50 出处:网络
I have a DropDownList with the following options: o s开发者_Python百科elect 1 hot 2 cold 3 warm How can I get the last option value (\"warm\") from the DropDownList?Assuming that you have a variabl

I have a DropDownList with the following options:

o s开发者_Python百科elect
1 hot
2 cold
3 warm

How can I get the last option value ("warm") from the DropDownList?


Assuming that you have a variable referenced to your DropDownList:

if (myDropDownList.Items.Count > 0)
{
    string myValue = myDropDownList.Items[myDropDownList.Items.Count - 1].Value;
}

Note that you should probably check that the DropDownList has items first, or else this will throw an IndexOutOfBounds exception when the list is empty. Thanks @Cylon.


var last = cmbMyList.Items.OfType<ListItem>().LastOrDefault();

(Thanks to Cylon Cat for correcting me)

Very Simple


Something like cboTemp.SelectedIndex = cboTemp.Items.Count -1;

0

精彩评论

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