ASP Net Controls ASP.Net Tutorial

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



    Command Button Test


   
   
      

Simple Calculator


      
         
                     OnCommand="Button_Command" CommandName="add" />
                     OnCommand="Button_Command" CommandName="subtract" />
         
         
                     OnCommand="Button_Command" CommandName="multiply" />
                     OnCommand="Button_Command" CommandName="divide" />
      

      
   

   


File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CommandTest : System.Web.UI.Page
{
  protected void Button_Command(Object o, CommandEventArgs e)
  {
      double dVal1 = 0.0;
      double dVal2 = 0.0;
      bool val1okay = Double.TryParse(txtValue1.Text, out dVal1);
      bool val2okay = Double.TryParse(txtValue2.Text, out dVal2);
     if (val1okay && val2okay)
     {
       double result = 0;
       string op = "";
       switch (e.CommandName)
       {
         case "add":
           op = "+";
           result = dVal1 + dVal2;
           break;
         case "subtract":
           op = "-";
           result = dVal1 - dVal2;
           break;
         case "multiply":
           op = "*";
           result = dVal1 * dVal2;
           break;
         case "divide":
           op = "/";
           result = dVal1 / dVal2;
           break;
       }
       labMessage.Text = txtValue1.Text + op + txtValue2.Text + "=" + result;
     }
     else
     {
       labMessage.Text = "Unable to compute a value with these values";
     }
  }
  
}