C# 将下拉列表中的选定值转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9924582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:15:08  来源:igfitidea点击:

converting selected value from dropdownlist into int

c#asp.net

提问by Bibu

I have a drop down list and I want the selected value to put into an int variable, then in my aspx page I want to assign it to a rowspan. This is my C# code for getting the value and converting it:

我有一个下拉列表,我希望将选定的值放入一个 int 变量中,然后在我的 aspx 页面中,我想将它分配给一个行跨度。这是我用于获取值并转换它的 C# 代码:

protected void drop_SelectedIndexChanged(object sender, EventArgs e)
        {
            int a = Int32.Parse(drop.SelectedValue.ToString());

And this is my aspx code where I'm trying to assign the variable a :

这是我的 aspx 代码,我试图在其中分配变量 a :

 <asp:TableHeaderRow>
 <asp:TableHeaderCell RowSpan="<% a %>">Hostese</asp:TableHeaderCell>
 </asp:TableHeaderRow>  

I get the error: cannot create an object of type int32 from its string representation. Can anyone say why? It's an asp.net application with C#.

我收到错误:无法从其字符串表示创建 int32 类型的对象。谁能说为什么?这是一个带有 C# 的 asp.net 应用程序。

采纳答案by ?zgür Kara

try setting this value when you read dropdown value.

当您读取下拉值时尝试设置此值。

<asp:TableHeaderRow>
    <asp:TableHeaderCell ID="h1" >Hostese</asp:TableHeaderCell>
</asp:TableHeaderRow> 



protected void drop_SelectedIndexChanged(object sender, EventArgs e)
{
    h1.RowSpan = Int32.Parse(drop.SelectedValue.ToString());

回答by ABH

If value of drop.SelectedValueisn't intthen you will get this error. For example if the value contains a floating point.

如果值drop.SelectedValue不是,int那么您将收到此错误。例如,如果该值包含一个浮点数。

回答by Jay

Try using int.Parse(drop.SelectedValue)or int.Parse(drop.SelectedValue.Trim())instead of Int32.Parse(drop.SelectedValue.ToString()). drop.SelectedValueis already in string format so you need not convert it using ToString

尝试使用int.Parse(drop.SelectedValue)int.Parse(drop.SelectedValue.Trim())代替Int32.Parse(drop.SelectedValue.ToString())drop.SelectedValue已经是字符串格式,所以你不需要使用ToString

回答by Bilal Hashmi

Try Int32.TryParsemethod, which try to convert string representation to int without throwing an exception. Also check the values of your drop down list items. This exception occurs when string value does not represent integer value.

TryInt32.TryParse方法,该方法尝试将字符串表示形式转换为 int 而不抛出异常。还要检查下拉列表项的值。当字符串值不代表整数值时会发生此异常。

回答by Ravi Ram

How about this:

这个怎么样:

int a = int.TryParse(drop.SelectedValue, out a)? a : 0;

回答by Armaan

if dropdown list items value is in numbers(digits) like...

如果下拉列表项的值是数字(数字),例如...

    <asp:DropDownList ID="DropDownList1" runat="server">
         <asp:ListItem Text="Please Select" Value="-1"></asp:ListItem>
         <asp:ListItem Text="1st" Value="1"></asp:ListItem>
         <asp:ListItem Text="2nd" Value="2"></asp:ListItem>
         <asp:ListItem Text="3rd" Value="3"></asp:ListItem>
     </asp:DropDownList>

then you can simply do this...

那么你可以简单地做到这一点......

int i = Int32.Parse(DropDownList1.SelectedValue);

This always works for me!!!!!!!!!!

这总是对我有用!!!!!!!!!!