单选控件和单选组控件(RadioButton and RadioButtonList).aspx <%...@ Page Language="C#" AutoEventWireup="true" CodeFile="单选控件和单选组控件(RadioButton and RadioButtonList).aspx.cs" Inherits="单选控件和单选组控件_RadioButton_and_RadioButtonList_" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>单选控件和单选组控件(RadioButton and RadioButtonList)</title></head><body> <form id="form1" runat="server"> <div> <br /> <br /> <br /> <p> 我们可以想象下,单选控件我们要实现的功能就是要在几个控件按钮里面实现选择一个我们可以来讨论这个问题首先我们要知道的是几个按钮放在一个界面里面,我们如何规定他是在一个组里面的呢,我们有一个属性值叫GroupName,将这个属性值设置成一样的时候我们就可以操作他一个组里面的结果RadioButton选出一个来。下面是一个范例: </p> <br /> <asp:RadioButton ID="RB1" runat="server" GroupName ="color" Text ="红色" AutoPostBack="True" OnCheckedChanged="RB1_CheckedChanged" /> <br /> <br /> <asp:RadioButton ID="RB2" runat="server" GroupName ="color" Text ="蓝色" AutoPostBack="True" OnCheckedChanged="RB2_CheckedChanged"/> <br /> <br /> <asp:RadioButton ID="RB3" runat="server" GroupName ="color" Text ="绿色" AutoPostBack="True" OnCheckedChanged="RB3_CheckedChanged"/> <br /> <br /> 如上我们可以看到他们三个在一个组里面,当我们对他们进行操作的时候就会触发相应的CheckedChanged事件 ,当运行的时候这个时候你会发现所有的事件都没有在你想象中运行他似乎没有对服务器进行回传,这个时候是新人很经常犯的错误,要将每个控件的属性设置为AutoPostBack=true<br /> <br /> <br /> <asp:RadioButtonList ID="RadioButtonList1" runat="server" Style="position: relative" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"> <asp:ListItem Value="1">红色背景</asp:ListItem> <asp:ListItem Value="2">蓝色背景</asp:ListItem> <asp:ListItem Value="3">绿色背景</asp:ListItem> </asp:RadioButtonList><br /> <br /> 观看RadioButtonList组控件我们可以想象他本来就在一个组里面,那么我们以前想的在一个组里面的groupname属性在这里没有了,RadioButtonList组控件有一个很好的好处就是他可以使用DataMemberDataSoure等属性与数据库相连接,可以动态的生成每一条记录,或者直接用Items属性,直接录入单选列表项<br /> <br /> <asp:RadioButtonList ID="RadioButtonList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="RadioButtonListname" DataValueField="RadioButtonListvalue" Style="position: relative" RepeatColumns ="2" RepeatDirection ="Horizontal" OnSelectedIndexChanged="RadioButtonList2_SelectedIndexChanged" > </asp:RadioButtonList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DBCONNECTIONSTRING %>" SelectCommand="SELECT [RadioButtonListname], [RadioButtonListvalue] FROM [RadioButtonList]"> </asp:SqlDataSource> <br /> 如上我们就是用数据库填充CheckBoxList的name和value 并且我将他的列数边成了两列还有就是他的数据以横向排列 事件处理的方法可以利用他的name和value 属性的值去描述SelectedIndexChanged事件里面的内容 这里的例子是选中后在上面显示选种的value and name值</div> </form></body></html> 单选控件和单选组控件(RadioButton and RadioButtonList).aspx.cs using System;using System.Data;using System.Configuration;using System.Collections;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;using System.Drawing;public partial class 单选控件和单选组控件_RadioButton_and_RadioButtonList_ : System.Web.UI.Page...{ protected void Page_Load(object sender, EventArgs e) ...{ } protected void RB1_CheckedChanged(object sender, EventArgs e) ...{ if (this.RB1.Checked == true) ...{ this.RB1.ForeColor = Color.Red; this.RB2.ForeColor = Color.Transparent; this.RB3.ForeColor = Color.Transparent; } } protected void RB2_CheckedChanged(object sender, EventArgs e) ...{ if (this.RB2.Checked == true) ...{ this.RB1.ForeColor = Color.Transparent; this.RB2.ForeColor = Color.Blue; this.RB3.ForeColor = Color.Transparent; } } protected void RB3_CheckedChanged(object sender, EventArgs e) ...{ if (this.RB3.Checked == true) ...{ this.RB1.ForeColor = Color.Transparent; this.RB2.ForeColor = Color.Transparent; this.RB3.ForeColor = Color.Green; } } protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) ...{ if (this.RadioButtonList1.SelectedIndex == 0) ...{ this.RadioButtonList1.BackColor = Color.Red; } else if (this.RadioButtonList1.SelectedIndex == 1) ...{ this.RadioButtonList1.BackColor = Color.Blue; } else ...{ this.RadioButtonList1.BackColor = Color.Green; } } protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e) ...{ Response.Write(this.RadioButtonList2 .SelectedValue ); Response.Write(this.RadioButtonList2.SelectedItem.Text); }}