views:

45

answers:

2

Hi, I have a DropDownList with a list of value.

I need in my code USE the value selected by the User in the DropDownList AS INT DataType (numeric).

At the moment I am using this code below (CONVERTING DATATYOES). Script work just fine.

But I would like to know if exists an alternative way to do it.

I am pretty new in ASP.NET any ideas it is very appreciate. Thanks for your time.


   <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;
+3  A: 

You can use the try parse method. This will make sure that your value is an int, and if not, allow you to handle the problem instead of throwing an exception:

int myPageSize;

if(int.TryParse(uxPageSizeUsersSelector.SelectedValue, out myPageSize))
{
     //SUCCESS
}
else
{
    ///handle problem here.
}

You can do it for Int16 DateTime etc. too.

Kevin
A: 

i think you have done basic part , you can just add some exception management technique.

use int.TryParse

saurabh