我只需要一种设置速率可以等于(我不想使用api)的方法,就可以将美元转换为欧元。
我以前从未使用过后端语言,所以我不确定该怎么做,但我认为该方法看起来像这样?
public void Convert(int rate, string fromDollars, string toEuros)
{
}
答案 0 :(得分:1)
好吧,您没有指定网络表单或mvc。
因此,让我们打开一个Web表单。我们将+拖放到两个组合框中(下拉菜单)。
一个文本框,一个按钮,我们有这个:
好吧,我们现在假设我们在sql server中有一个这样的表:
好,现在是我们的代码。
在页面加载“事件”时,我们填充了两个组合框(我们的从到货币)。
页面加载时,代码看起来像这样(我们填充了两个组合框)。
' on first page load only, load up combo boxes
If IsPostBack = False Then
Dim strSQL = "SELECT ToUsDollars, Country FROM tblCurrency ORDER BY Country"
Dim MyData As DataTable = Myrst(strSQL)
cFrom.DataSource = MyData
cFrom.DataBind()
cTo.DataSource = MyData
cTo.DataBind()
End If
按钮的代码如下:
Dim curAmount As Decimal = txtFromAmount.Text
Dim curResult As Decimal = curAmount / cFrom.SelectedValue
' we have amount in usa dollars, now conver to target amount
Me.txtResultAmount.Text = curResult * cTo.SelectedValue
因此,我们采用任何一种货币,将其转换为一种已知货币(美国),然后将其简单地转换为目标货币。
结果页面如下:
因此,它没有太多代码。方便的花花公子“ MyRst”只是一个例程,它使用我们的sql字符串并返回数据表。
它看起来像这样:
Public Function Myrst(strSQL As String) As DataTable
Dim strCon As String = ConfigurationManager.ConnectionStrings("Test4").ConnectionString
Dim rstData As New DataTable
Using mycon As New SqlConnection(strCon)
Dim cmdSQL As New SqlCommand(strSQL, mycon)
mycon.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
Return rstData
End Function
所以上面显示了:
加载组合的代码(dropDownList) 以及单击按钮时运行的按钮代码(服务器端.net代码)。
因此,这主要是“拖放”设计方法,然后编写代码以加载组合,并使用一个按钮的代码来计算结果。
因此,以上内容应使您了解asp.net的代码工作原理
是通过拖放形式的desinger吗?
生成的标记看起来像这样:
(我确实添加了两个“ div”,以使两个组合框彼此并排)。 没什么漂亮的-只需几分钟即可完成。
<body>
<form id="form1" runat="server">
<div>
<div style="float:left">
<asp:Label ID="Label1" runat="server" Text="From"></asp:Label>
<br />
<asp:DropDownList ID="cFrom" runat="server" Width="131px" DataTextField="Country" DataValueField="ToUsDollars" Height="17px"></asp:DropDownList>
</div>
<div style="float:left;padding-left:30px">
<asp:Label ID="Label2" runat="server" Text="To"></asp:Label>
<br />
<asp:DropDownList ID="cTo" runat="server" Width="135px" DataTextField="Country" DataValueField="ToUsDollars" Height="20px"></asp:DropDownList>
<br />
</div>
<div style="clear:both;float:left;text-align:center">
<br />
<h2>enter amount</h2>
<asp:TextBox ID="txtFromAmount" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Calulate" Width="111px" />
<h2>Result</h2>
<asp:TextBox ID="txtResultAmount" runat="server" Width="183px" Font-Size="Large"></asp:TextBox>
</div>
</div>
</form>
</body>