简体   繁体   English

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

[英]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.

相关问题 ASP.Net 如何将与下拉列表值相关的信息从数据库显示到文本框中? - ASP.Net how can I display information related to dropdown list value into a textbox from database? 如何在 ASP.NET Core MVC 中的 Edit 方法的下拉字段中显示 selectedValue(来自数据库) - How can I display the selectedValue (from database) in a dropdown-field from the Edit method in ASP.NET Core MVC 如何通过显示文本在ASP.NET下拉菜单中设置所选项目? - How can you set the selected item in an ASP.NET dropdown via the display text? 如何在asp.net中基于多个选定的下拉项显示Gridview记录 - How can i display gridview records based on multi selected dropdown items in asp.net 如何从asp.net的文本框中获取多个值 - how to get multiple values from text-box in asp.net 如何在 ASP.NET 中显示来自 C# 的警报框? - How to display an alert box from C# in ASP.NET? 我如何在asp.net的下拉复选框标题框中显示所选项目 - how can i display selected items on the dropdowncheckboxes caption box in asp.net 我无法从我的ASP.NET MVC3 ViewModel获取我的月,日,年下拉列表 - I can't get my Month, Day, Year dropdown lists to work from my ASP.NET MVC3 ViewModel 如何在asp.net的“多选”下拉文本框中以逗号分隔的形式显示所选项目 - How can i show the selected items as comma separated in the Multi select dropdown text field in asp.net asp.net-如何在较长的过程中异步更新文本框 - asp.net - How can I update a text box asynchronously during a long process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM