[英]How can I multiply values from 2 dropdown lists in asp.net and display the results in a text box?
I have a lot of dropdown lists I am building for an insurance app. 我有很多为保险应用程序构建的下拉列表。 Each list has a number of options given a multiplier value. 每个列表都有给定乘数的多个选项。 My question is how can I multiply these values from separate dropdown lists to give myself a final multiplier value in a textbox? 我的问题是如何将这些值从单独的下拉列表中相乘,从而在文本框中为自己提供最终的乘数? I want to use whatever value is picked from each dropdown list. 我想使用从每个下拉列表中选取的任何值。
The lists are formatted as follows 列表格式如下
<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>
I have only been coding for about 2 weeks so hopefully somebody can point me in the right direction. 我只编码了大约2周,所以希望有人可以指出正确的方向。 I have not found anything similar in the search here. 我在这里的搜索中没有找到任何类似的东西。 Thanks guys! 多谢你们!
It's important to remember that the values that are going to be stored within your DropDownLists will be strings, so the first step will be converting them to numerical values, which can be done in many ways, but for example purposes, you can use the Convert.ToDecimal()
method : 重要的是要记住,将要存储在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();
If you have more DropDownLists or other elements that would need to be part of this calculation, you would just need to ensure that you parse each of their values and then include them in your calculation. 如果您有更多DropDownLists或其他元素需要包含在此计算中,则只需确保您解析它们的每个值,然后将它们包括在计算中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.