Header Ads Widget

ASP.NET practical-6

 

_ASP.NET

PRACTICAL-6


AIM: Create a Simple calculator with validations and details.

 

.aspx code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Practical_6.WebForm1" %>

    <!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">
        <title></title>
    </head>

    <body>
        <form id="form1" runat="server">
            <div>

                <asp:TextBox ID="txtA" runat="server"></asp:TextBox>

                <asp:RequiredFieldValidator ID="rqr1" runat="server" ControlToValidate="txtA" ErrorMessage="Enter A"
                    ForeColor="Red" />

                <asp:RangeValidator ID="rng1" runat="server" ControlToValidate="txtA" Type="Double" MinimumValue="0.0"
                    MaximumValue="65536.0" ErrorMessage="Invalid value" ForeColor="Red" />

                <asp:TextBox ID="txtB" runat="server"></asp:TextBox>

                <asp:RequiredFieldValidator ID="rqr2" runat="server" ControlToValidate="txtB" ErrorMessage="Enter B"
                    ForeColor="Red" />

                <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtB" Type="Double"
                    MinimumValue="0.0" MaximumValue="65536.0" ErrorMessage="InvalidB" ForeColor="Red" />

                <asp:DropDownList ID="drpOp" runat="server">
                    <asp:ListItem>+</asp:ListItem>

                    <asp:ListItem>-</asp:ListItem>

                    <asp:ListItem>*</asp:ListItem>

                    <asp:ListItem>/</asp:ListItem>

                    <asp:ListItem>%</asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button2" runat="server" Text="Calculate" />
                <asp:Label ID="lblAnswer" runat="server"></asp:Label>

            </div>
        </form>
    </body>

    </html>


.aspx.vb code:-

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

        If CInt(txtB.Text) = 0 Then

            Response.Write("Cannot divide by 0")

        ElseIf drpOp.Text = "+" Then

            lblAnswer.Text = CDbl(txtA.Text) + CDbl(txtB.Text)

        ElseIf drpOp.Text = "-" Then

            lblAnswer.Text = txtA.Text - txtB.Text

        ElseIf drpOp.Text = "*" Then

            lblAnswer.Text = txtA.Text * txtB.Text

        ElseIf drpOp.Text = "/" Then

            lblAnswer.Text = txtA.Text / txtB.Text

        ElseIf drpOp.Text = "%" Then

            lblAnswer.Text = txtA.Text Mod txtB.Text

        End If
    End Sub 
End Class


Output:-





















Post a Comment

0 Comments