单选控件和单选组控件

本文介绍了ASP.NET中RadioButton与RadioButtonList控件的基本用法及实例,探讨了如何通过GroupNames属性创建单选组,并展示了如何使用RadioButtonList与数据库交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

单选控件和单选组控件(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选出一个来。下面是一个范例:
&nbsp;</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
&nbsp;并且我将他的列数边成了两列还有就是他的数据以横向排列 事件处理的方法可以利用他的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);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值