CheckBox
CheckBox
| Property | Description |
| AutoPostBack | A Boolean value that specifies whether the form should be posted immediately after the Checked property has changed or not. Default is false |
| Checked | A Boolean value that specifies whether the check box is checked or not |
| id | A unique id for the control |
| OnCheckedChanged | The name of the function to be executed when the Checked property has changed |
| runat | Specifies that the control is a server control. Must be set to "server" |
| Text | The text next to the check box |
| TextAlign | On which side of the check box the text should appear (right or left) |
| Try this: |
| 1 | <script runat="server"> |
| 2 | Sub Check(sender As Object, e As EventArgs) |
| 3 | if check1.Checked then |
| 4 | work.Text=home.Text |
| 5 | else |
| 6 | work.Text="" |
| 7 | end if |
| 8 | End Sub |
| 9 | </script> |
| 10 | <html> |
| 11 | <body> |
| 12 | <form runat="server"> |
| 13 | <p>Home Phone: |
| 14 | <asp:TextBox id="home" runat="server" /> |
| 15 | <br /> |
| 16 | Work Phone: |
| 17 | <asp:TextBox id="work" runat="server" /> |
| 18 | <asp:CheckBox id="check1" Text="Same as home phone" TextAlign="Right" AutoPostBack="True" OnCheckedChanged="Check" runat="server" /> |
| 19 | </p> |
| 20 | </form> |
| 21 | </body> |
| 22 | </html> |
| Save as: Checkbox.aspx |
- Open Visual Studio
- Add a new webform
- Name it checkbox.aspx
- Click the Add button
- Select the Design View
- Add 2 labels and 2 textbox controls
- Add a checkbox control
- Change the text of the labels to:
Work Phone:
- Change the id property of the textboxes to:
Work
- Change the id of the checkbox to:
- Change the text of the checkbox to:
- Change the textAlign property to right
- Change AutoPostBack to true
- Double-click the checkbox
- Add this code:
If Check1.Checked Then
work.Text = home.Text
Else
work.Text = ""
End If
work.Text = home.Text
Else
work.Text = ""
End If
- Test it by pressing CTRL + F5