The CheckBox control use we to display, well, a check box. We can use the CheckBox control in a newsletter signup form.
Syntax:
<asp:CheckBox ID="chkNewsletter" runat="server" />
The CheckBox control supports the following properties (this is not a complete list):
S.N. |
Property |
Description |
1) |
ID |
Set the uniqe value to control to access in code-behind code. |
2) |
Text |
This use for set or get value from or to CheckBox control. |
3) |
AccessKey |
This property used to specify a key that navigates to the CheckBox control. |
4) |
TextAlign |
Align the label for the check box. Possible values are Left and Right. |
5) |
AutoPostBack |
Enables we to post the form containing the check box back to the server automatically when the CheckBox is checked or unchecked. |
6) |
Enabled |
This used to enable or disable the CheckBox control. |
7) |
TabIndex |
Used to set the tab order of the check box. |
The CheckBox control also supports the following method:
S.N. |
Method |
Description |
1) |
Focus |
Used to set the initial form focus on it. |
The CheckBox control supports the following event:
S.N. |
Event |
Description |
1) |
CheckedChanged |
Raised on the server when the check box is checked or unchecked. |
When the AutoPostBack property has the value True, the form containing the check box is
automatically posted back to the server when the check box is checked or unchecked.
Code: CheckBoxDemo01.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckBoxDemo01.aspx.cs"
Inherits="LearnAsp.Net.Control.CheckBox.CheckBoxDemo01" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="chkNewsletter" Text="Receive Newsletter?" runat="server" />
<br />
<asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server" />
<hr />
<asp:Label ID="lblResult" runat="server" />
</div>
</form>
</body>
</html>
Code: CheckBoxDemo01.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LearnAsp.Net.Control.CheckBox
{
public partial class CheckBoxDemo01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblResult.Text = chkNewsletter.Checked.ToString();
}
}
}
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.