CHECKBOX CONTROL IN ASP.NET

CHECKBOX CONTROL IN ASP.NET

CheckBox control is an asp.net web server control. CheckBox control appears as a square on web forms. CheckBox control allows the user to check and uncheck in a square. If the square in the CheckBox control is ticked, checked = true and unchecked, then checked = false.


We can drag the CheckBox control from the toolbox and show it on the web form as shown below. CheckBox control allows the user to either check (tick) or unchecks (untick) the Checkbox control in ASP.NET.


CHECKBOX CONTROL IN ASP.NET

Example of CheckBox Control in ASP.Net C #


To understand whether the checkbox is checked or unchecked, let's take an example synced to the checkbox in a simple asp.net.


CHECKBOX CONTROL IN ASP.NET


Here is the HTML design source code of the web page in asp.net: -


<body>

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

<div>

<table align="center" class="style1" style="border: thin solid #008080">

<tr>

<td class="style2" style="text-align: center; border-bottom-style: solid; border-bottom-width: thin; border-bottom-color: #008080;">

CheckBox Control in ASP.Net</td>

</tr>

<tr>

<td style="text-align: center">

</td>

</tr>

<tr>

<td style="text-align: center">

<asp:CheckBox ID="CheckBox1" runat="server" Text="RED" />

</td>

</tr>

<tr>

<td style="text-align: center">

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Select" />

</td>

</tr>

<tr>

<td style="text-align: center">

<asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label>

</td>

</tr>

</table>

</div>

</form>

</body>{codeBox}


After designing an asp.net web page with a checkbox, the button and label control appears on the screen below. We did not check the checkbox in the Asp.net figure given below. When the select button is clicked, we get a response in the label control that the checkbox is unchecked.


CHECKBOX CONTROL IN ASP.NET


Now, check the checkbox control and then click on the button to display the message in the label, you can display any message on the label like - "CheckBox is Checked"


Here is the C # code on the button click event to check the checkbox, stating that the checkbox is checked or uncheck.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


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

{

    protected void Page_Load(object sender, EventArgs e)

    {


    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (CheckBox1.Checked == true)

        {

            Label1.Text = "CheckBox is Checked";

        }

        else

        {

            Label1.Text = "CheckBox is unchecked";

        }

    }

}{codeBox}


Checkbox  Property Autopostback


Autopostback means when we receive a request from the user and go to the server with the request and forward the request to the server and get back to the user with the resulting response, all-round trips known as postbacks.


If we set Autopostback = true to the check box then the checkbox control enables the CheckedChanged event for the user, that is, we go backward in code when checking or unchecking the checkbox control.


The example below shows how auto postback will work in a checkbox control. Design an asp.net web page with two checkbox controls with one label control. 


Set both checkbox controls to AutoPostBack = true.


CheckBox Control AutoPostBack Example in ASP.Net


<body>

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

    <div>

    

        <table align="center" class="style1" style="border: thin solid #008080">

            <tr>

                <td class="style2" 

                    style="text-align: center; border-bottom-style: solid; border-bottom-width: thin; border-bottom-color: #008080;">

                    CheckBox Control in ASP.Net</td>

            </tr>

            <tr>

                <td style="text-align: center">

 

                     

                    </td>

            </tr>

            <tr>

                <td style="text-align: center">

                    <asp:CheckBox ID="chkred" runat="server" AutoPostBack="True" 

                        oncheckedchanged="chkred_CheckedChanged" style="font-weight: 700" Text="RED" />

 

                     

                    <asp:CheckBox ID="chkgreen" runat="server" AutoPostBack="True" 

                        oncheckedchanged="chkgreen_CheckedChanged" style="font-weight: 700" 

                        Text="GREEN" />

                </td>

            </tr>

            <tr>

                <td style="text-align: center">

                     </td>

            </tr>

            <tr>

                <td style="text-align: center">

                    <asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label>

                </td>

            </tr>

            </table>

    

    </div>

    </form>

</body>

</html>{codeBox}


Here, we can check only one checkbox at a time, if we check one checkbox and try to check another checkbox then the first checkbox control is automatically unchecked using checkbox_CheckedChanged events.


The screen below shows the design output of the asp.net checkbox auto postback instance.


CHECKBOX CONTROL IN ASP.NET


Check the code below for both checkbox CheckedChanged events.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


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

{

    protected void Page_Load(object sender, EventArgs e)

    {


    }

    protected void chkred_CheckedChanged(object sender, EventArgs e)

    {

        Label1.Text = "RED CheckBox is checked";

        chkgreen.Checked = false;

    }

    protected void chkgreen_CheckedChanged(object sender, EventArgs e)

    {

        Label1.Text = "GREEN CheckBox is checked";

        chkred.Checked = false;

    }

}{codeBox}


In the example below, when we check the Red checkbox control, the message "RED CheckBox is checked" is displayed on the label control. And if we check the green checkbox control then the red checkbox control is automatically unchecked and the label control displays the checked message with a green checkbox.


CHECKBOX CONTROL IN ASP.NET
AKASH TIMES

I AM AN ENGINEER BY PROFESSION BUT BLOGGER IS MY OLD DAYS DREAM TO CREATE MY OWN SITE FOR THOSE WHO ARE CURIOUS ABOUT MY BIRTHPLACE THEN I AM COMING FROM (INDIA). THE CURRENT CITY LIVES IN NAVI MUMBAI, INDIA

Post a Comment

IF YOU HAVE ANY PROBLEM PLEASE FELL FREE TO CONTACT US BY - AKASH TIMES

PREVIOUS POST NEXT POST