- 一个下拉列表货币转换器 -

时间:2010-02-01 03:39:03

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

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

1 个答案:

答案 0 :(得分: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#)