ASP.NET Tutorial/ASP.net Controls/CheckBox
Содержание
asp: CheckBox changed event (C#)
File: Default.aspx
<%@ Page language="c#" Inherits="EventTracker" CodeFile="Default.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Event Tracker</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<h1>Controls being monitored for change events:</h1>
<asp:TextBox ID="txt"
runat="server"
AutoPostBack="true"
OnTextChanged="CtrlChanged" />
<br /><br />
<asp:CheckBox ID="chk"
runat="server"
AutoPostBack="true"
OnCheckedChanged="CtrlChanged"/>
<br /><br />
<asp:RadioButton ID="opt1" runat="server" GroupName="Sample"
AutoPostBack="true" OnCheckedChanged="CtrlChanged"/>
<asp:RadioButton ID="opt2" runat="server" GroupName="Sample"
AutoPostBack="true" OnCheckedChanged="CtrlChanged"/>
<br /><br /><br />
<h1>List of events:</h1>
<asp:ListBox ID="lstEvents" runat="server" Width="355px"
Height="305px" /><br />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class EventTracker : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
Log("<< Page_Load >>");
}
private void Log(string entry)
{
lstEvents.Items.Add(entry);
lstEvents.SelectedIndex = lstEvents.Items.Count - 1;
}
protected void Page_PreRender(object sender, System.EventArgs e)
{
Log("Page_PreRender");
}
protected void CtrlChanged(Object sender, EventArgs e)
{
string ctrlName = ((Control)sender).ID;
Log(ctrlName + " Changed");
}
}
Get selected CheckBox
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="CheckBox" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
What are your favorite ice cream flavors?<br />
<br />
<asp:CheckBox ID="A" runat="server" Text="A" /><br />
<br />
<asp:CheckBox ID="B" runat="server" Text="B" /><br />
<br />
<asp:CheckBox ID="C" runat="server" Text="C" /><br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Click Me" /><br />
<br />
<asp:Label ID="results" runat="server"></asp:Label></div>
</form>
</body>
</html>
File: Default.aspx.vb
Partial Class CheckBox
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
results.Text = ""
If A.Checked Then
results.Text &= "A."
End If
If B.Checked Then
results.Text &= "B."
End If
If C.Checked Then
results.Text &= "C."
End If
End Sub
End Class
Import properties, events and methods of CheckBox control
AccessKey: specify a key that navigates to the TextBox control.
AutoPostBack: post the form containing the CheckBox back to the server automatically when the CheckBox is checked or unchecked.
Checked: get or set whether the CheckBox is checked.
Enabled: disable the TextBox.
TabIndex: specify the tab order of the check box.
Text: provide a label for the check box.
TextAlign: align the label for the check box. Possible values are Left and Right.
Focus: set the initial form focus to the check box.
CheckedChanged: Raised on the server when the check box is checked or unchecked.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblResult.Text = chkNewsletter.Checked.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show CheckBox</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>
use the AutoPostBack property to post the form to the server automatically when the check box is checked or unchecked.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void chkNewsletter_CheckedChanged(object sender, EventArgs e)
{
lblResult.Text = chkNewsletter.Checked.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>CheckBox AutoPostBack</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox
id="chkNewsletter"
Text="Receive Newsletter?"
AutoPostBack="true"
OnCheckedChanged="chkNewsletter_CheckedChanged"
Runat="server" />
<hr />
<asp:Label
id="lblResult"
Runat="server" />
</div>
</form>
</body>
</html>