Saturday, August 29, 2020

Checkbox control in asp.net

 What is checkbox in asp.net?

A Checkbox control are used to display a check box that allows the user to select a true or false condition. 

Checkbox control inherited from : 

Object->Control->WebControl->Checkbox

The following example demonstrates how to use CheckBox Control to indicate whether discount apply or not on given amount. 

Create a web page and add the following code : 

<!DOCTYPE html>

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

<head runat="server">

    <title></title>

</head>

<body>

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

        <div>

            <table style="padding:1rem;">

                <tr>

                    <td>

                     <b>   Orignal amount :</b>

                    </td>

                    <td>

                        1500 Rs.

                    </td>

                  

                </tr>

                <tr>

                     <td>

                       <b> 10% Discount</b>

                    </td>

                    <td>

                        <asp:CheckBox ID="chkBox" runat="server" OnCheckedChanged="OnCheckedChanged_Clicked" AutoPostBack="true" />

                    </td>

                </tr>

            </table>

            <asp:Label ID ="lblAfterDiscount" runat="server"></asp:Label>

        </div>

    </form>

</body>

</html>


Add following code in cs file 

using System;

namespace tutorials

{

    public partial class Checkbox : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

       

        protected void OnCheckedChanged_Clicked(object sender,EventArgs e)

        {

            float orignalAmount = 1500;

            if (chkBox.Checked)

            {

                float Discount = (1500 / 100) * 10;

                orignalAmount = orignalAmount - Discount;

                lblAfterDiscount.Text = "Amount after 10% discount : "+orignalAmount;

            }

            else

            {

                lblAfterDiscount.Text = "Amount without discount : " + orignalAmount;

            }

        }

    }

}




No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...