ASP.NET Tutorial/ASP.net Controls/RadioButton — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:30, 26 мая 2010

asp:RadioButton 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 RadioButton

<%@ 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">
    <div>
        What is your favorite ice cream flavor?
        <br />
        <br />
        <asp:RadioButton ID="A" GroupName="flavors" runat="server" Text="A" /><br />
        <br />
        <asp:RadioButton ID="B" GroupName="flavors" runat="server" Text="B" /><br />
        <br />
        <asp:RadioButton ID="C" GroupName="flavors" 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 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


Important properties, methods and events for RadioButton

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">
    <div>
    How did you hear about our Website?
    <ul>
        <li>
        <asp:RadioButton
            id="rdlMagazine"
            Text="Magazine Article"
            GroupName="Source"
            Runat="server" />
        </li>
        <li>
        <asp:RadioButton
            id="rdlTelevision"
            Text="Television Program"
            GroupName="Source"
            Runat="server" />
        </li>
        <li>
        <asp:RadioButton
            id="rdlOther"
            Text="Other Source"
            GroupName="Source"
            Runat="server" />
        </li>
    </ul>
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" OnClick="btnSubmit_Click" />
    <hr />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Use AutoPostBack property to detect which RadioButton control is selected.

<%@ 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">
    <div>
    How did you hear about our Website?
    <ul>
        <li>
        <asp:RadioButton
            id="rdlMagazine"
            Text="Magazine Article"
            GroupName="Source"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged"
            Runat="server" />
        </li>
        <li>
        <asp:RadioButton
            id="rdlTelevision"
            Text="Television Program"
            GroupName="Source"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged"
            Runat="server" />
        </li>
        <li>
        <asp:RadioButton
            id="rdlOther"
            Text="Other Source"
            GroupName="Source"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged"
            Runat="server" />
        </li>
    </ul>
    <hr />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Use RadioButton to set font size

<%@ 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">
    <div>
    <h1>RadioButton Control</h1>
     <br />
     <asp:Label ID="lblTime" runat="server" OnInit="lblTime_Init"></asp:Label>
     
     <br />
     <br />
     <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" />
    </div>
    </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();
   }
 }