views:

158

answers:

1

Hi, 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...

+1  A: 

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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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#)

slugster
thanks a lot!!!=) it's ok...=D
tohru
ohya,I wanna ask, is it possible that we get the label rate and convert it to the selected dropdownlist...
tohru
Yes it is possible, check my amended post which includes a textbox, when you type a value in it and move the focus away from it the equivalent value in the dropdown is selected.
slugster
You should also note that if this is the answer to your posted question, then you should mark it as the answer. Try not to morph the original question, instead ask a new question. Thanks.
slugster