I had to check this out. What could be the difference if the developer were to use KO and not ASP.Net?
The only difference is the programming language of use - C# or jQuery / Javascript.
The resultant output is this below
No button click event just the AutoPostBack property of the drop down list set to true.
I will first share the ASP.Net code and then dwell into the equivalent KO code.
You need JSon.Net installed and referenced to the project, an app_id to consume the JSon data resultset from openexchangerates.org, VS 2012 for web and you are good to go.
The HTML source of the .aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetExchangeRates.aspx.cs" Inherits="GetExchangeRates" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Currency
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
Here is the aspx code behind.
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
public partial class GetExchangeRates : System.Web.UI.Page
{
static JSon_SampleData.CurrencyRates obj;
Dictionary rateValues;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
obj = new JSon_SampleData.CurrencyRates();
obj = Download_Serialized_ExchangeRate("http://openexchangerates.org/api/latest.json?app_id=");
rateValues = new Dictionary();
rateValues = obj.Rates;
foreach (var raate in rateValues)
{
DropDownList1.Items.Add(raate.Key);
}
Session["rateValues"] = rateValues;
}
}
private static T Download_Serialized_ExchangeRate(string url) where T : new()
{
using (var w = new WebClient())
{
var json_data = string.Empty;
try
{
json_data= w.DownloadString(url);
}
catch (Exception) { }
return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject(json_data) : new T();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
rateValues = (Dictionary)Session["rateValues"];
foreach (var raate in rateValues)
{
if (raate.Key == DropDownList1.SelectedItem.Text)
{
TextBox1.Text = raate.Value.ToString();
break;
}
}
}
}
And the Class to hold the JSon data.
using System.Collections.Generic;
namespace JSon_SampleData
{
public class CurrencyRates
{
public string Disclaimer { get; set; }
public string License { get; set; }
public int TimeStamp { get; set; }
public string Base { get; set; }
public Dictionary Rates { get; set; }
}
}
Reference the JSon Data class in your ASP.Net website.
I will post the KO code in the next part and get back to the BDD and Agile part.
The only difference is the programming language of use - C# or jQuery / Javascript.
The resultant output is this below
No button click event just the AutoPostBack property of the drop down list set to true.
I will first share the ASP.Net code and then dwell into the equivalent KO code.
You need JSon.Net installed and referenced to the project, an app_id to consume the JSon data resultset from openexchangerates.org, VS 2012 for web and you are good to go.
The HTML source of the .aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetExchangeRates.aspx.cs" Inherits="GetExchangeRates" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Currency
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
Here is the aspx code behind.
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
public partial class GetExchangeRates : System.Web.UI.Page
{
static JSon_SampleData.CurrencyRates obj;
Dictionary
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
obj = new JSon_SampleData.CurrencyRates();
obj = Download_Serialized_ExchangeRate
rateValues = new Dictionary
rateValues = obj.Rates;
foreach (var raate in rateValues)
{
DropDownList1.Items.Add(raate.Key);
}
Session["rateValues"] = rateValues;
}
}
private static T Download_Serialized_ExchangeRate
{
using (var w = new WebClient())
{
var json_data = string.Empty;
try
{
json_data= w.DownloadString(url);
}
catch (Exception) { }
return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
rateValues = (Dictionary
foreach (var raate in rateValues)
{
if (raate.Key == DropDownList1.SelectedItem.Text)
{
TextBox1.Text = raate.Value.ToString();
break;
}
}
}
}
And the Class to hold the JSon data.
using System.Collections.Generic;
namespace JSon_SampleData
{
public class CurrencyRates
{
public string Disclaimer { get; set; }
public string License { get; set; }
public int TimeStamp { get; set; }
public string Base { get; set; }
public Dictionary
}
}
I will post the KO code in the next part and get back to the BDD and Agile part.
No comments:
Post a Comment