Home Interesting Facts Checkbox control in an ASP.NET | Computer Hindi Notes

Checkbox control in an ASP.NET | Computer Hindi Notes

0
171
Checkbox control in ASP.NET |  Computer Hindi Notes

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 uncheck (untick) the Checkbox control in ASP.NET.

Example of CheckBox Control in ASP.Net C #

In order to understand whether the checkbox is checked or unchecked, let’s take an example synced to the checkbox in a simple 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>

After designing an asp.net web page with a checkbox, the button and label control appear as 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.

aspnet checkbox control properties

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";
        }
    }
}

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 post backs .

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

The example below shows how autopostback 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

Keyboard Function Keys (F1-F12) : How to Master Keyboard?

<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>

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

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

asp net checkbox autopostback

Check the code below for both checkbox CheckedChanged event.

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;
    }
}

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 automatically gets unchecked and the label control displays the checked message with green checkbox.

.

No Comments

Leave A Reply

Please enter your comment!
Please enter your name here