简体   繁体   English

下拉列表值:从字符串转换为整数,然后再转换回字符串并显示在文本框中

[英]dropdownlist value: converting from string to int then back to string and display in textbox

I have multiple dropdown lists.我有多个下拉列表。 I'm trying to have dropdown list input values to be added together and that the answer be displayed in a textbox.我正在尝试将下拉列表输入值添加在一起,并将答案显示在文本框中。 I'm converting the values into integers for the arithmetic operations and then back into strings to display the answer in the textbox.我将这些值转换为用于算术运算的整数,然后再转换回字符串以在文本框中显示答案。

It builds without errors in Visual Studio and when loaded in the browser.它在 Visual Studio 中以及在浏览器中加载时都不会出错。 But when you select one dropdownlist an error will occur: "Input string was not in the correct format."但是当您选择一个下拉列表时,会出现错误:“输入字符串的格式不正确。” For the life of me I can't find the error with the code:对于我的生活,我找不到代码的错误:

displayRed is the Textbox i want the answer to be displayed in Red1DD, Red2DD etc. are the dropdownlists. 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();

this is what one of the dropdownlists look like:这是下拉列表之一的样子:

<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>

you should use Red1DD.SelectedItem.Text;你应该使用Red1DD.SelectedItem.Text; instead of SelectedValue.ToString()而不是SelectedValue.ToString()

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();

Maybe the problem is, that you define a default value of empty string for each of your dropdowns.也许问题是,您为每个下拉列表定义了一个默认值空字符串。 <asp:ListItem Selected="True" Value="" Text="--" />

Now when you change the selection of one of the dropdowns to 0 or 1, then the values of the other dropdowns are still empty strings.现在,当您将下拉列表之一的选择更改为 0 或 1 时,其他下拉列表的值仍然是空字符串。 Convert.ToInt32(..) does not like an empty string as argument an throws an Exception. Convert.ToInt32(..)不喜欢空字符串作为参数并抛出异常。

Therfore change your default value to 0/1 or you have to handle empty strings in your "convert logic".因此,将您的默认值更改为 0/1,否则您必须在“转换逻辑”中处理空字符串。 For example Convert.ToInt32(string.IsNullOrEmpty(Red1DD.SelectedValue.ToString()) ? "0" : Red1DD.SelectedValue.ToString());例如Convert.ToInt32(string.IsNullOrEmpty(Red1DD.SelectedValue.ToString()) ? "0" : Red1DD.SelectedValue.ToString()); and so on.等等。

(You can create a method, which checks if the string is empty and returns the parsed value or zero if emtpy.) (您可以创建一个方法,该方法检查字符串是否为空并返回解析的值,如果为空则返回零。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM