neI有一个带有值列表的下拉列表。
我需要在我的代码中使用用户在DropDownList中选择的值作为INT数据类型(数字)。
目前我正在使用下面的代码(转换数据类型)。
但是 我想知道是否有另一种方法 .
任何想法都非常感谢。谢谢你的时间。
<asp:DropDownList ID="uxPageSizeUsersSelector" runat="server" AutoPostBack="True" onselectedindexchanged="uxPageSizeUsersSelector_SelectedIndexChanged"> <asp:ListItem Value="1" Selected="True">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> <asp:ListItem Value="3">3</asp:ListItem> <asp:ListItem Value="4">4</asp:ListItem> </asp:DropDownList>
int myPageSize = Convert.ToInt16(uxPageSizeUsersSelector.SelectedValue.ToString()); PageSize = myPageSize;
您可以使用try parse方法。这将确保您的值是int,如果不是,则允许您处理该问题,而不是引发异常:
int myPageSize; if(int.TryParse(uxPageSizeUsersSelector.SelectedValue, out myPageSize)) { //SUCCESS } else { ///handle problem here. }
你也可以在Int16日期时间等。