【问题标题】:-One dropdownlist currency converter--一个下拉列表货币转换器-
【发布时间】:2010-02-01 03:39:02
【问题描述】:

我想通过使用一个下拉列表来转换我的费率字段。例如,如果在日本选择下拉列表,当用户选择并更改为马来西亚时,汇率字段将自动从日本汇率更改为马来西亚汇率。有人吗?...谢谢...

【问题讨论】:

  • 请指定编程语言。
  • 你有任何代码吗?
  • asp.net vb 语言,不..我没有那个代码

标签: asp.net vb.net drop-down-menu


【解决方案1】:

下拉列表有两个值 - 文本和值。您已将下拉列表绑定到一组项目(可能在 IEnumerable 中,如数组或列表)。因此,您所要做的就是在客户端拦截 onchange 事件,获取下拉列表的选定值,并将其放入显示速率的标签/文本框中。下面是一个例子:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" onchange="javascript:PopulateRate(this.value);"></asp:DropDownList>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" onchange="javascript:SelectRate(this.value);" style="width: 100px;"></asp:TextBox>
    </div>
    </form>
</body>

    <script language="javascript">
        function PopulateRate(value) {
            //debugger;
            document.getElementById('<% =Label1Name() %>').innerText = value;
        }
        function SelectRate(value) {
            var z = document.getElementById('<% =DropDownList1Name() %>');
            //method 1 to set dropdown selected item:
            z.value = value;
            //method 2 to set dropdown selected item::
            for (var i = 0; i < z.options.length; i++) {
                if (z.options[i].value == value) {
                    z.options[i].selected = true;
                    return;
                }
            } 
        }
    </script>

</html>


Partial Class _Default
Inherits System.Web.UI.Page

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)

    Dim countryRates = New System.Collections.Generic.Dictionary(Of String, Decimal)

    countryRates.Add("Japan", 1.0)
    countryRates.Add("Malaysia", 1.5)
    countryRates.Add("Khazakstan", 1.75)
    countryRates.Add("Argentina", 2.0)
    countryRates.Add("Andorra", 2.5)

    DropDownList1.DataTextField = "Key"
    DropDownList1.DataValueField = "value"
    DropDownList1.DataSource = countryRates
    DropDownList1.DataBind()
End Sub

Protected Property Label1Name() As String
    Get
        Return Label1.UniqueID
    End Get
    Set(ByVal value As String)

    End Set
End Property
Protected Property DropDownList1Name() As String
    Get
        Return DropDownList1.UniqueID
    End Get
    Set(ByVal value As String)

    End Set
End Property
End Class

根据您的描述,这显示了如何做您想做的事。我只是想让你知道,在 VB.Net 中做这个示例让我很头疼 :)(所以你必须原谅我的劣质 VB 代码,我通常使用 C#)

【讨论】:

  • ohya,我想问一下,我们是否有可能获得标签费率并将其转换为选定的下拉列表...
  • 是的,有可能,检查我修改后的帖子,其中包含一个文本框,当您在其中键入一个值并将焦点从它移开时,下拉列表中的等效值被选中。
  • 您还应该注意,如果这是您发布的问题的答案,那么您应该将其标记为答案。尽量不要改变原来的问题,而是提出一个新问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
相关资源
最近更新 更多