GridView中有两列,每列一个RadioButton,选中其一后不允许选另外一个并提示

本文介绍了一个使用ASP.NET的GridView控件结合RadioButton实现特定交互功能的例子。通过客户端脚本控制RadioButton的选择,并记录用户的操作,适用于需要进行二选一的情况。

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!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>无标题页</title>

    
<script type="text/javascript">
        
function AlertMes(obj){
           
var txt = document.getElementById("txtChecked");
           
var rb = eval("document.getElementById('form1')."+obj.name);
           
var hideValue = ";"+obj.name+obj.id;
           
if(txt.value.indexOf(obj.name) == -1){
              txt.value 
+= ";"+hideValue;
            }
            
else if(txt.value.indexOf(hideValue) == -1)
            {
                 
if(rb[0].checked){
                        rb[
1].checked = true;
                        alert(
'已选择第二列');
                    }
                    
else{
                        rb[
0].checked=true;
                        alert(
'已选择第一列');
                    }
            }
        }
    
</script>

</head>
<body>
    
<form id="form1" runat="server">
    
<input type="hidden" id="txtChecked" />
    
<div>
        
<asp:GridView ID="GridView1" runat="server">
            
<Columns>
                
<asp:TemplateField>
                    
<ItemTemplate>
                        
<asp:RadioButton runat="server"  GroupName="a" ID="rb1" onclick="AlertMes(this)">
                        
</asp:RadioButton>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:TemplateField>
                    
<ItemTemplate>
                        
<asp:RadioButton runat="server" GroupName="a" ID="rb2" onclick="AlertMes(this)">
                        
</asp:RadioButton>
                    
</ItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
    
</div>
    
</form>
</body>
</html>

using System;
using System.Data;

public partial class Default6 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
new DataColumn("Name"));
            dt.Rows.Add(
new object[] { "111" });
            dt.Rows.Add(
new object[] { "111" });
            
this.GridView1.DataSource = dt;
            
this.GridView1.DataBind();
        }
    }
}

转载于:https://www.cnblogs.com/Ferry/archive/2009/08/21/1551488.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值