Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# dropdownlist值:从字符串转换为int,然后再转换为字符串并显示在文本框中_C#_String_Drop Down Menu_Integer_Selectedvalue - Fatal编程技术网

C# dropdownlist值:从字符串转换为int,然后再转换为字符串并显示在文本框中

C# dropdownlist值:从字符串转换为int,然后再转换为字符串并显示在文本框中,c#,string,drop-down-menu,integer,selectedvalue,C#,String,Drop Down Menu,Integer,Selectedvalue,我有多个下拉列表。我正在尝试将下拉列表中的输入值添加到一起,并将答案显示在文本框中。我将这些值转换成整数进行算术运算,然后再转换成字符串,在文本框中显示答案 它在VisualStudio中生成且在浏览器中加载时不会出错。但是,当您选择一个dropdownlist时,会出现一个错误:“输入字符串的格式不正确。”在我的一生中,我无法找到代码中的错误: displayRed是我希望答案显示在其中的文本框 Red1DD、Red2DD等是下拉列表 displayRed.Text = (Convert.

我有多个下拉列表。我正在尝试将下拉列表中的输入值添加到一起,并将答案显示在文本框中。我将这些值转换成整数进行算术运算,然后再转换成字符串,在文本框中显示答案

它在VisualStudio中生成且在浏览器中加载时不会出错。但是,当您选择一个dropdownlist时,会出现一个错误:“输入字符串的格式不正确。”在我的一生中,我无法找到代码中的错误:

displayRed是我希望答案显示在其中的文本框 Red1DD、Red2DD等是下拉列表

displayRed.Text =
  (Convert.ToInt32(Red1DD.SelectedValue.ToString())
 + Convert.ToInt32(Red2DD.SelectedValue.ToString())
 + Convert.ToInt32(Red4DD.SelectedValue.ToString())
 + Convert.ToInt32(Red8DD.SelectedValue.ToString())
 + Convert.ToInt32(Red16DD.SelectedValue.ToString())
 + Convert.ToInt32(Red32DD.SelectedValue.ToString())
 + Convert.ToInt32(Red64DD.SelectedValue.ToString())
 + Convert.ToInt32(Red128DD.SelectedValue.ToString())).ToString();
这是其中一个下拉列表的外观:

<asp:DropDownList ID="Red1DD" runat="server" AutoPostBack="True" OnSelectedIndexChanged="red_SelectedIndexChanged">
        <asp:ListItem Selected="True" Value="" Text="--" />
        <asp:ListItem value="0" Text="0" />
        <asp:ListItem value="1" Text="1" />
</asp:DropDownList>

您应该使用
Red1DD.SelectedItem.Text
而不是
SelectedValue.ToString()


可能问题是,您为每个下拉列表定义了一个默认值空字符串。

现在,当您将其中一个下拉列表的选择更改为0或1时,其他下拉列表的值仍然是空字符串。
Convert.ToInt32(..)
不喜欢空字符串作为参数,因为参数会引发异常

因此,请将默认值更改为0/1,否则您必须在“转换逻辑”中处理空字符串。 例如
Convert.ToInt32(string.IsNullOrEmpty(Red1DD.SelectedValue.ToString())?“0”:Red1DD.SelectedValue.ToString())
等等


(您可以创建一个方法,检查字符串是否为空并返回解析值,如果为emtpy,则返回零。)

当您第一次打开页面时,您的DropDownList是否初始化为整数值?DropDownList如何进行数据绑定?为什么不将组合的
列表设置为
数据源
,然后使用
(int)为算术选择EdItem,然后在最终结果上选择ToString()
?否当您第一次打开页面时,my DropDownList未初始化为整数值。有一个值为“”的初始“-”选项。没什么。有什么办法吗?所选值和文本完全不同…SelectedItem.text只提供显示的文本,而不是项目的值
displayRed.Text =
  (Convert.ToInt32(Red1DD.SelectedItem.Text)
 + Convert.ToInt32(Red2DD.SelectedItem.Text)
 + Convert.ToInt32(Red4DD.SelectedItem.Text)
 + Convert.ToInt32(Red8DD.SelectedItem.Text)
 + Convert.ToInt32(Red16DD.SelectedItem.Text)
 + Convert.ToInt32(Red32DD.SelectedItem.Text)
 + Convert.ToInt32(Red64DD.SelectedItem.Text)
 + Convert.ToInt32(Red128DD.SelectedItem.Text)).ToString();