0

我想在http://www.webservicex.com/CurrencyConvertor.asmx?wsdl使用货币转换器。但它不起作用我在下拉列表菜单中什么都没有。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.webservicex.www;

public partial class cc : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //using web service at http://www.webservicex.com/CurrencyConvertor.asmx?wsdl
        double rate = 0, result = 0;
        Currency curr1, curr2;
        try
        {
            CurrencyConvertor ws = new CurrencyConvertor();
            curr1 = (Currency)Enum.Parse(typeof(Currency), DropDownList1.SelectedItem.Value);
            curr2 = (Currency)Enum.Parse(typeof(Currency), DropDownList2.SelectedItem.Value);
            rate = ws.ConversionRate(curr1, curr2);
            result = System.Convert.ToDouble(TextBox1.Text) * rate;
            Label2.Text = "The exchange rate between" + DropDownList1.SelectedItem.Text + " and " + DropDownList2.SelectedItem.Text + " is " + rate;
            Label3.Text = "Converted " + TextBox1.Text + " to " + result;
        }
        catch
        {
            Label2.Text = "Web service not available please try again later."; 
        }
    }
}

谢谢

4

1 回答 1

0

我建议避免在每次要显示下拉菜单时调用 Web 服务。货币的选择不会经常改变。(无论如何,您都需要准备好处理错误。)

相反,预先下载一次可用的数据并将其存储在适合支持下拉列表的 XML 文件中:

<Currencies>
     <currency Name="EUR" />
     <currency Name="RUB" />

然后,有 ASP.NET 代码如下:

<asp:XmlDataSource ID="XmlDataSource1" runat="server"
        DataFile="~/Currencies.xml">
</asp:XmlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
        DataSourceID="XmlDataSource1" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>
于 2012-05-16T22:19:07.303 回答