CheckBox control in ASP.NET using VB.NET

In this article we will learn how to use CheckBox in ASP. NET.
  • 3572

In this article we will learn how to use CheckBox in ASP. NET.

CheckBox control:

The CheckBox control is used to display a check box. The CheckBox control gives us an option to select true/false. A checkbox is clicked to select and clicked again to deselect some option. When a checkbox is selected, a check (a tick mark) appears indicating a selection.

Properties:

checkbox1.gif
 

Figure 1.

Text: text level shown with the CheckBox.

Checked: Specifies whether the check box is checked or not.

AutoPostBack: Automatically posts back to the server when the control is clicked.

TextAlign: On which side of the check box the text should appear (right or left).

AccessKey: Keyboard shortcut to used by control.

CausesValidation: Specifies if a page is validated when a Button control is clicked

CheckBox event:

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox1.CheckedChanged

End Sub

For example:

Drag two CheckBoxes, a TextBox and a Button control on to the form. Set the Text property for the CheckBoxes as Male and Female. The following code will display the text of the Checkbox that is checked in the textbox when the button is clicked. Form looks like this:

checkbox2.gif
 

Figure 2.

Now double click on the button control and add the following code.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        If CheckBox1.Checked Then

            TextBox1.Text = "Your Gender" & " " & CheckBox1.Text

        ElseIf CheckBox2.Checked Then

            TextBox1.Text = "Your gender" & " " & CheckBox2.Text

        End If

    End Sub

 

Now save and run the application.

checkbox3.gif
 

Figure 3.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.