开发者

Easier way to get last item in Dropdownlist

开发者 https://www.devze.com 2022-12-08 19:37 出处:网络
There\'s got to be an easier way to do what I came up with here: int lastDayIn开发者_JAVA技巧List = ddlBirthDay.Items.IndexOf(ddlBirthDay.Items[ddlBirthDay.Items.Count -1]);

There's got to be an easier way to do what I came up with here:

int lastDayIn开发者_JAVA技巧List = ddlBirthDay.Items.IndexOf(ddlBirthDay.Items[ddlBirthDay.Items.Count -1]);


This will give you the last item in a drop down list.

ListItem lastItem = ddlBirthDay.Items[ddlBirthDay.Items.Count-1]

In your code, it looks like you are getting the index of the last item. However, the accessible index of the last item will be -1 of the count.


If you are using .net v3.5 then Linq is an option, too.

ListItem latItem = ddlBirthday.Items.Last();
ListItem latItem = ddlBirthday.Items.LastOrDefault();
0

精彩评论

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