Pages

Subscribe:

Ads 468x60px

Tuesday, March 6, 2012

Currency Converter in asp.net using c#


Adding the Currency Converter Code
To actually add some functionality to the currency converter, you need to add some ASP.NET
code. Web forms are event-driven, which means every piece of code acts in response to a specific
event. In the simple currency converter page example, the most useful event occurs when
the user clicks the submit button (named Convert). The HtmlInputButton allows you to react
to this action by handling the ServerClick event.
Before you continue, it makes sense to add another control that can display the result of
the calculation. In this case, you can use a <div> tag named Result. The <div> tag is one way to
insert a block of formatted text into a web page. Here’s the HTML that you’ll need:
...

The style attribute applies the CSS properties used to format the text. In this example, it
merely applies a bold font.
The example now has the following four server controls:

  • • A submit button named Convert (HtmlInputButton object). • A <div> tag named Result (HtmlGenericContro
A form (HtmlForm object). This is the only control you do not need to access in your
code-behind class.
  • Listing 5-1 shows the revised web page (CurrencyConverter.aspx), but leaves out the doctype t
  • An input text box named US (HtmlInputText object).
  • l object).
  • o save space. Listing 5-2 shows the code-behind class (CurrencyConverter.aspx.cs). The
  • code-behind class includes an event handler that reacts when the convert button is clicked. It
  • calculates the currency conversion and displays the result.


code for CurrencyConverter.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>


Currency Converter


140 CHAPTER 5 n WEB FORM FUNDAMENTALS
Convert:     U.S. dollars to Euros.




code for CurrencyConverter.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class CurrencyConverter : System.Web.UI.Page
{
protected void Convert_ServerClick(object sender, EventArgs e)
{
decimal USAmount = Decimal.Parse(US.Value);
decimal euroAmount = USAmount * 0.85M;
Result.InnerText = USAmount.ToString() + " U.S. dollars = ";
Result.InnerText += euroAmount.ToString() + " Euros.";
}
}

1 comment:

Anna Schafer said...

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. convert money calculator

Post a Comment