ASP.NET Tutorial/ASP.net Controls/RadioButton
Содержание
asp:RadioButton 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 RadioButton
<source lang="csharp">
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="test" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
What is your favorite ice cream flavor?<asp:Label ID="results" runat="server"></asp:Label>
<asp:RadioButton ID="A" GroupName="flavors" runat="server" Text="A" />
<asp:RadioButton ID="B" GroupName="flavors" runat="server" Text="B" />
<asp:RadioButton ID="C" GroupName="flavors" runat="server" Text="C" />
<asp:Button ID="btnSubmit" runat="server" Text="Click Me" />
</form>
</body> </html>
File: Default.aspx.vb
Partial Class test
Inherits System.Web.UI.Page Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click If A.Checked Then results.Text = "A" ElseIf B.Checked Then results.Text = "B" ElseIf C.Checked Then results.Text = "C" End If End Sub
End Class</source>
Important properties, methods and events for RadioButton
<source lang="csharp">
AccessKey: specify a key that navigates to the RadioButton control. AutoPostBack: post the form containing the RadioButton back to the server automatically when the radio button is checked or unchecked. Checked: get or set whether the RadioButton control is checked. Enabled: disable the RadioButton. GroupName: group RadioButton controls. TabIndex: specify the tab order of the RadioButton control. Text: label the RadioButton control. TextAlign: align the RadioButton label. Possible values are Left and Right. Focus: set the initial form focus to the RadionButton control. CheckedChanged: Raised on the server when the RadioButton 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) { if (rdlMagazine.Checked) lblResult.Text = rdlMagazine.Text; if (rdlTelevision.Checked) lblResult.Text = rdlTelevision.Text; if (rdlOther.Checked) lblResult.Text = rdlOther.Text; }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Show RadioButton</title>
</head> <body>
<form id="form1" runat="server">
How did you hear about our Website?
- <asp:RadioButton id="rdlMagazine" Text="Magazine Article" GroupName="Source" Runat="server" />
- <asp:RadioButton id="rdlTelevision" Text="Television Program" GroupName="Source" Runat="server" />
- <asp:RadioButton id="rdlOther" Text="Other Source" GroupName="Source" Runat="server" />
<asp:Button id="btnSubmit" Text="Submit" Runat="server" OnClick="btnSubmit_Click" />
<asp:Label id="lblResult" Runat="server" />
</form>
</body> </html></source>
Use AutoPostBack property to detect which RadioButton control is selected.
<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 RadioButton_CheckedChanged(object sender, EventArgs e) { RadioButton selectedRadioButton = (RadioButton)sender; lblResult.Text = selectedRadioButton.Text; }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>RadioButton AutoPostBack</title>
</head> <body>
<form id="form1" runat="server">
How did you hear about our Website?
- <asp:RadioButton id="rdlMagazine" Text="Magazine Article" GroupName="Source" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" Runat="server" />
- <asp:RadioButton id="rdlTelevision" Text="Television Program" GroupName="Source" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" Runat="server" />
- <asp:RadioButton id="rdlOther" Text="Other Source" GroupName="Source" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" Runat="server" />
<asp:Label id="lblResult" Runat="server" />
</form>
</body> </html></source>
Use RadioButton to set font size
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!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 runat="server">
<title>RadioButton Control</title>
</head> <body>
<form id="form1" runat="server">
RadioButton Control
<asp:Label ID="lblTime" runat="server" OnInit="lblTime_Init"></asp:Label>
<asp:RadioButton ID="rdoSize10" runat="server" GroupName="grpSize" AutoPostBack="True" Text="10pt" OnCheckedChanged="grpSize_CheckedChanged" /> <asp:RadioButton ID="rdoSize14" runat="server" GroupName="grpSize" AutoPostBack="True" Text="14pt" OnCheckedChanged="grpSize_CheckedChanged" /> <asp:RadioButton ID="rdoSize16" runat="server" GroupName="grpSize" AutoPostBack="True" Text="16pt" OnCheckedChanged="grpSize_CheckedChanged" />
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default_aspx : System.Web.UI.Page {
protected void grpSize_CheckedChanged(object sender, EventArgs e) { if (rdoSize10.Checked) lblTime.Font.Size = 10; else if (rdoSize14.Checked) lblTime.Font.Size = 14; else lblTime.Font.Size = 16; } protected void lblTime_Init(object sender, EventArgs e) { lblTime.Font.Name = "Verdana"; lblTime.Font.Size = 20; lblTime.Font.Bold = true; lblTime.Font.Italic = true; lblTime.Text = DateTime.Now.ToString(); } }</source>