-One dropdownlist currency converter-

I wanted to convert my rate field by using one dropdownlist to convert it. Eg, if the dropdownlist selected on japan, when the user select and change to malaysia the rate field will automatically change to malaysia rate from japan rate. Anyone?… Thanks…

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Dropdown lists have two values – the text and the value. You have either bound your dropdown list to a set of items (probably in an IEnumerable like an array or List). So all you have to do is intercept the onchange event on the client side, grab the selected value of the dropdown, and place it into the label/textbox that shows the rate. Here is an example for you:

<%@ 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

From your description, this shows how to do what you want. I just want you to know that it hurt my head to have to do this sample in VB.Net 🙂 (so you will have to excuse my shoddy VB code, i usually do C#)

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply