zoukankan      html  css  js  c++  java
  • 嵌套的在GridView里的radiobutton 怎么才能只选中一个

    GridView1  中有一列从数据库加载数据  ,并且加一列模板列,在编辑模板中  给该列加上radiobutton1  

    但是运行起来后   那列所有radiobutton可被选中请问怎样处理才能让只选中一个阿!!!

    这是今天在CSDN上看见有个朋友问的问题,我看见有的人说指定同一个GroupName属性,但是这个不管用,有人说用HTML控件代替服务器端控件,我觉得HTML空间不灵活,服务器端根据选择结果处理的时候不太好用,还有人说用JavaScript遍历真个GridView,我觉得这样也不好。我后来想了一个觉得跟前面几个比都要好一点的办法,用服务器端控件但是有不去遍历GridView的方法:
    在服务器端给 GridView添加如下方法

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            
    {
                
    if (e.Row.RowType == DataControlRowType.DataRow)
                
    {
                    RadioButton rb 
    = (RadioButton)e.Row.FindControl("RadioButton1");
                    rb.Attributes.Add(
    "onclick""judge(this)");//给RadioButton添加onclick属性
                }

            }

    在ASPX页面里添加下面的JS脚本
        <script type="text/javascript">
            
    var last = null;//最后访问的RadioButton的ID
            
    function judge(obj)
            
    {
                
    if(last == null)
                
    {
                    last 
    = obj.id;
               
    //     alert(last);
                }

                
    else
                
    {
                    
    var lo = document.getElementById(last);
                    lo.checked 
    = "";
             
    //       alert(last + "  " + lo.checked);
                    last = obj.name;
                }

                obj.checked 
    = "checked";
            }

        
    </script>
    这样就行了!呵呵,我觉得的至少比便利GridView要好吧!
  • 相关阅读:
    laravel migrate 指定执行部分 migration
    Laravel attribute casting 导致的 Indirect modification of overloaded property
    python中列表,元组的乘法
    python中简化的验证码功能
    Day4作业:蛋疼CRM系统
    python中pop(),popitem()的整理
    新手pyhoner的指定内容读取和写入的思路
    Python基础之文件操作
    Python基础之内置函数(一)
    Python基础之set集合与函数
  • 原文地址:https://www.cnblogs.com/interboy/p/700956.html
Copyright © 2011-2022 走看看