繁体   English   中英

如何在asp.net中将2个下拉列表中的值相乘并在文本框中显示结果?

[英]How can I multiply values from 2 dropdown lists in asp.net and display the results in a text box?

我有很多为保险应用程序构建的下拉列表。 每个列表都有给定乘数的多个选项。 我的问题是如何将这些值从单独的下拉列表中相乘,从而在文本框中为自己提供最终的乘数? 我想使用从每个下拉列表中选取的任何值。

列表格式如下

<asp:DropDownList id="points" runat="server">
     <asp:ListItem Value="1.00">0</asp:ListItem>
     <asp:ListItem Value="1.05">1</asp:ListItem>
     <asp:ListItem Value="1.10">2</asp:ListItem>
     <asp:ListItem Value="1.18">3</asp:ListItem>
     <asp:ListItem Value="1.25">4</asp:ListItem>
     <asp:ListItem Value="1.32">5</asp:ListItem>
     <asp:ListItem Value="1.40">6</asp:ListItem>
     <asp:ListItem Value="1.47">7</asp:ListItem>
     <asp:ListItem Value="1.55">8</asp:ListItem>
     <asp:ListItem Value="1.62">9</asp:ListItem>
     <asp:ListItem Value="1.70">10</asp:ListItem>
     <asp:ListItem Value="1.77">11</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="OtherPolicy" runat="server">
     <asp:ListItem value="1.20">Yes</asp:ListItem>
     <asp:ListItem value="1.00">No</asp:ListItem>
</asp:DropDownList>

我只编码了大约2周,所以希望有人可以指出正确的方向。 我在这里的搜索中没有找到任何类似的东西。 多谢你们!

重要的是要记住,将要存储在DropDownLists中的值将是字符串,因此第一步是将它们转换为数值,这可以通过多种方式完成,但是出于示例目的,您可以使用Convert.ToDecimal()方法:

// Convert your Points to a decimal
var pointsValue = Convert.ToDecimal(points.SelectedValue);

// Then convert your selection from your other DropDownList
var policyValue = Convert.ToDecimal(OtherPolicy.SelectedValue);

// Now that you have both of these, you can multiply them to retrieve your result
var result = pointsValue * policyValue;

// Store your result in another TextBox
YourResultTextBox.Text = result.ToString();

如果您有更多DropDownLists或其他元素需要包含在此计算中,则只需确保您解析它们的每个值,然后将它们包括在计算中即可。

暂无
暂无

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

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