CheckBox - ( ASP.NET Web Forms )
The CheckBox control is used to represent a checkbox input element on a web form. It allows the user to select one or more options from a list of options.
Syntax
Here is the basic syntax for a CheckBox control:
<asp:CheckBox runat="server" ID="checkBoxName" Text="CheckBox Text" Checked="true/false" />
Example
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<asp:CheckBox runat="server" ID="checkBox1" Text="Option 1" />
<asp:CheckBox runat="server" ID="checkBox2" Text="Option 2" />
<br />
<asp:Button runat="server" ID="submitButton" OnClick="submit_Click" Text="Submit" />
</form>
</body>
</html>
Output
The above example will display a web page with two checkboxes and a "Submit" button. When the user selects one or more checkboxes and clicks the "Submit" button, a postback occurs and the code in the Click event handler of the button is executed.
Explanation
The CheckBox control has several properties that you can use to customize its appearance and behavior. These properties include ID, Text, Checked, CssClass, and Enabled.
The ID property is used to give the control an identifier that can be used to reference it from code-behind. The Text property sets the label for the checkbox. The Checked property determines whether the checkbox is checked by default. The CssClass property is used to apply CSS styles to the control. The Enabled property indicates whether the control is enabled or disabled.
Use
The CheckBox control can be used in a variety of web forms to enable the user to select one or more options from a list of options. This is often used in forms that require the user to select one or more items from a list before proceeding.
Important Points
- The CheckBox control represents a checkbox input element on a web form.
- The Checked property determines whether the checkbox is checked by default.
- The Text property sets the label for the checkbox.
- The button_click event handler is executed when the user clicks the submit button.
Summary
The CheckBox control is a useful element for web forms that require the user to select one or more options from a list of options. In this page, we discussed the syntax, an example, output, explanation, use, important points, and summary of the CheckBox control. By learning about these properties and how to use them, you can create more interactive and dynamic web forms.