Read input from text field, do calculation then output to the save page (C#) : Text input « HTML Controls « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » HTML Controls » Text input 
4. 15. 2. Read input from text field, do calculation then output to the save page (C#)
File: Default.aspx


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Currency Converter</title>
  </head>
  <body>
    <form ID="Form1" method="post" runat="server">
        <div>
        Convert:
        <input type="text" ID="US" runat="server"/>U.S. dollars to
        <select ID="Currency" runat="server" />
        <br /><br />
        <input type="submit" value="OK" ID="Convert" runat="server" OnServerClick="Convert_ServerClick" />
        <input type="submit" value="Show Graph" ID="ShowGraph" runat="server" OnServerClick="ShowGraph_ServerClick" />
        <br /><br />
        <img ID="Graph" alt="Currency Graph" scr="" runat="server" />
        <br /><br />
        <div style="font-weight: bold" ID="Result" runat="server"></div>
      </div>
    </form>
  </body>
</html>


File: Default.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 Page_Load(Object sender, EventArgs e)
  {
    if (this.IsPostBack == false)
    {
      Currency.Items.Add(new ListItem("Euros""0.5"));
      Currency.Items.Add(new ListItem("Japanese Yen""100"));
      Currency.Items.Add(new ListItem("Canadian Dollars""2"));
    }
    Graph.Visible = false;
  }
    protected void Convert_ServerClick(object sender, EventArgs e)
    {
        decimal amount;
        bool success = Decimal.TryParse(US.Value, out amount);
        if (success)
        {
            ListItem item = Currency.Items[Currency.SelectedIndex];

            decimal newAmount = amount * Decimal.Parse(item.Value);
            Result.InnerText = amount.ToString() " U.S. dollars = ";
            Result.InnerText += newAmount.ToString() " " + item.Text;
        }
        else
        {
            Result.InnerText = "The number you typed in was not in the correct format. ";
            Result.InnerText += "Use only numbers.";
        }
    }

    protected void ShowGraph_ServerClick(object sender, EventArgs e)
    {
        Graph.Src = "Pic" + Currency.SelectedIndex.ToString() ".png";
        Graph.Visible = true;
    }
}
4. 15. Text input
4. 15. 1. Set value to html:textbox in page load event (VB.net)
4. 15. 2. Read input from text field, do calculation then output to the save page (C#)
4. 15. 3. TextBox server changed event
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.