ASP.NET Tutorial/ASP.net Controls/CheckBox
Содержание
asp: CheckBox changed event (C#)
<source lang="csharp">
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">
Controls being monitored for change events:
<asp:TextBox ID="txt" runat="server" AutoPostBack="true" OnTextChanged="CtrlChanged" />
<asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="CtrlChanged"/>
<asp:RadioButton ID="opt1" runat="server" GroupName="Sample" AutoPostBack="true" OnCheckedChanged="CtrlChanged"/> <asp:RadioButton ID="opt2" runat="server" GroupName="Sample" AutoPostBack="true" OnCheckedChanged="CtrlChanged"/>
List of events:
<asp:ListBox ID="lstEvents" runat="server" Width="355px" Height="305px" />
</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"); }
}</source>
Get selected CheckBox
<source lang="csharp">
<%@ 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">
What are your favorite ice cream flavors?<asp:Label ID="results" runat="server"></asp:Label>
<asp:CheckBox ID="A" runat="server" Text="A" />
<asp:CheckBox ID="B" runat="server" Text="B" />
<asp:CheckBox ID="C" runat="server" Text="C" />
<asp:Button ID="btnSubmit" runat="server" Text="Click Me" />
</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</source>
Import properties, events and methods of CheckBox control
<source lang="csharp">
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">
<asp:CheckBox id="chkNewsletter" Text="Receive Newsletter?" Runat="server" />
<asp:Button id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" Runat="server" />
<asp:Label id="lblResult" Runat="server" />
</form>
</body> </html></source>
use the AutoPostBack property to post the form to the server automatically when the check box is checked or unchecked.
<source lang="csharp">
<%@ 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">
<asp:CheckBox id="chkNewsletter" Text="Receive Newsletter?" AutoPostBack="true" OnCheckedChanged="chkNewsletter_CheckedChanged" Runat="server" />
<asp:Label id="lblResult" Runat="server" />
</form>
</body> </html></source>