Skip to main content

How to use CheckBox control in asp.net c#


CheckBox Server Control



The CheckBox is an asp.net web server control. A CheckBox allows the website visitor to select (check) a true or false condition. So the CheckBox control creates a CheckBox on the web forms page which allow the user to switch between a true or false state. CheckBox Text property creates a caption for it. We can left or right-align the caption by using the CheckBox TextAlign property.




The CheckBox control has many properties that help us to change looks and styles such as ForeColor, BorderColor, BackColor, BorderStyle, BorderWidth, CssClass, EnableTheming, Font, Height, SkinID, ToolTip, Width, etc.




If we want to determine whether CheckBox is checked (selected) then we need to test the CheckBox Checked property. CheckBox AutoPostBack property value true enable automatic posting to the server. CheckBox CheckedChanged event is raised when someone changes the CheckBox true or false state. The .NET developers can write an event handler for the CheckedChanged event. Using the CheckedChanged event we can know the CheckBox's current state and can perform any task when the page post to the server.




The following example demonstrates to us how to use CheckBox control in asp.net. Here we create a web form with a Label and two CheckBox control. When someone checks the first CheckBox then the label shows a message that he checked first CheckBox. The second CheckBox programmatically checked when user checked the first CheckBox.




CheckBoxExample.aspx



<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
protected void CheckBox1_CheckChanged(object sender, System.EventArgs e) {

if (CheckBox1.Checked == true) {
Label1.Text = "WOW! You are a member of an asp.net user group.";
Label1.ForeColor = System.Drawing.Color.Green;
}
else{
Label1.Text = "You are not a member of any asp.net user group.";
Label1.ForeColor = System.Drawing.Color.Crimson;
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to use CheckBox control in asp.net</title>
</head>

<body style="padding:25px">
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
How to use CheckBox control
</h2>
<hr width="450" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
Font-Names="Comic Sans MS"
ForeColor="Crimson"
Font-Italic="true"
Font-Size="X-Large"
Width="350"
/>
<br /><br />
<asp:CheckBox
ID="CheckBox1"
runat="server"
Text="Are you an asp.net user group member?"
OnCheckedChanged="CheckBox1_CheckChanged"
AutoPostBack="true"
Font-Names="Serif"
Font-Size="X-Large"
/>
</div>
</form>
</body>
</html>












Popular posts from this blog

Restricting Jetpack Compose TextField to Numeric Input Only

Jetpack Compose has revolutionized Android development with its declarative approach, enabling developers to build modern, responsive UIs more efficiently. Among the many components provided by Compose, TextField is a critical building block for user input. However, ensuring that a TextField accepts only numeric input can pose challenges, especially when considering edge cases like empty fields, invalid characters, or localization nuances. In this blog post, we'll explore how to restrict a Jetpack Compose TextField to numeric input only, discussing both basic and advanced implementations. Why Restricting Input Matters Restricting user input to numeric values is a common requirement in apps dealing with forms, payment entries, age verifications, or any data where only numbers are valid. Properly validating input at the UI level enhances user experience, reduces backend validation overhead, and minimizes errors during data processing. Compose provides the flexibility to implement ...

jetpack compose - TextField remove underline

Compose TextField Remove Underline The TextField is the text input widget of android jetpack compose library. TextField is an equivalent widget of the android view system’s EditText widget. TextField is used to enter and modify text. The following jetpack compose tutorial will demonstrate to us how we can remove (actually hide) the underline from a TextField widget in an android application. We have to apply a simple trick to remove (hide) the underline from the TextField. The TextField constructor’s ‘colors’ argument allows us to set or change colors for TextField’s various components such as text color, cursor color, label color, error color, background color, focused and unfocused indicator color, etc. Jetpack developers can pass a TextFieldDefaults.textFieldColors() function with arguments value for the TextField ‘colors’ argument. There are many arguments for this ‘TextFieldDefaults.textFieldColors()’function such as textColor, disabledTextColor, backgroundColor, cursorC...

jetpack compose - Image clickable

Compose Image Clickable The Image widget allows android developers to display an image object to the app user interface using the jetpack compose library. Android app developers can show image objects to the Image widget from various sources such as painter resources, vector resources, bitmap, etc. Image is a very essential component of the jetpack compose library. Android app developers can change many properties of an Image widget by its modifiers such as size, shape, etc. We also can specify the Image object scaling algorithm, content description, etc. But how can we set a click event to an Image widget in a jetpack compose application? There is no built-in property/parameter/argument to set up an onClick event directly to the Image widget. This android application development tutorial will demonstrate to us how we can add a click event to the Image widget and make it clickable. Click event of a widget allow app users to execute a task such as showing a toast message by cli...