<asp:TemplateColumn HeaderText="结  果" ItemStyle-Width = "70">
                                <ItemTemplate>
                                    <input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
                                        
                                        
                                    <asp:RadioButtonList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
                                                    Height="1px" RepeatDirection="Vertical" >
                                                    <asp:ListItem>合格</asp:ListItem>
                                                    <asp:ListItem>不合格</asp:ListItem>
                                                    
                                                </asp:RadioButtonList>
                                                                       
                                    
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                            </asp:TemplateColumn>嵌入datagrid中某列的RadioButtonList

解决方案 »

  1.   

    在合格與不合格之外再加一個別的選項
    或者寫成checkbox用js在客戶端進行單選控制
      

  2.   

    寫成checkbox用js在客戶端進行單選控制这个怎么做?
      

  3.   

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script language="javascript">
        window.onload = function(){
            var obj = null;
            var chks = document.getElementById("<%=ItemCheckResult.ClientID %>").getElementsByTagName("input");
            for(var i=0;i<chks.length;i++){
                chks[i].onclick=function(){
                    if(this.checked){
                        if(obj!=null && obj!=this)obj.checked=false;
                        obj = this;
                    }
                }
            }
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
     
        
               <asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" 
                                Height="1px" RepeatDirection="Vertical" > 
                                <asp:ListItem>合格 </asp:ListItem> 
                                <asp:ListItem>不合格 </asp:ListItem> 
                                <asp:ListItem>优秀 </asp:ListItem> 
                            </asp:CheckBoxList>     </form>
    </body>
    </html>
      

  4.   

    谢谢 cutbug,我的 CheckBoxList 是放在datagrid中的一列,编译的时候报错误如下:错误 33 当前上下文中不存在名称“ItemCheckResult”
    <asp:DataGrid ID="DG_Data">
    <Columns>
    <asp:TemplateColumn HeaderText="结  果">
    <ItemTemplate>
    <input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
    <asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" 
                                Height="1px" RepeatDirection="Vertical" > 
                                <asp:ListItem>合格 </asp:ListItem> 
                                <asp:ListItem>不合格 </asp:ListItem> 
                                <asp:ListItem>优秀 </asp:ListItem> 
                            </asp:CheckBoxList> </ItemTemplate>
    <HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
    <ItemStyle Width="70px" />
    </asp:TemplateColumn></Columns>
    </asp:DataGrid>
      

  5.   

    最简单的做法就是加个选项 none
      

  6.   

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script language="javascript">
        function eforCheck(gridID,checkListID){
            var obj = null;
            var grid = document.getElementById(gridID);
            if(grid==null) return;
            var tables = grid.getElementsByTagName("table");
            for(var i=0;i<tables.length;i++){
                if(tables[i].id.indexOf(checkListID)!=-1){
                    var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
                    for(var j=0;j<chks.length;j++){
                        chks[j].onclick=function(){
                            if(this.checked){
                                if(obj!=null && obj!=this)obj.checked=false;
                                obj = this;
                            }
                        };
                    }
                }
            }
        }
       
        window.onload=function(){
            eforCheck("<%=DG_Data.ClientID%>","ItemCheckResult");
        };
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:DataGrid ID="DG_Data" runat="server" OnLoad="DG_Data_Load"> 
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" 
                                            Height="1px" RepeatDirection="Vertical" > 
                            <asp:ListItem>合格 </asp:ListItem> 
                            <asp:ListItem>不合格 </asp:ListItem> 
                            <asp:ListItem>优秀 </asp:ListItem> 
                     </asp:CheckBoxList> 
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
        </asp:DataGrid>                    
        </form>
    </body>
    </html>
      

  7.   


        <script>
        function singleChecked()
        {    
            var p=event.srcElement.parentNode;   
                 
            var input=p.getElementsByTagName('input');    
                
            for(var i=0;i<input.length;i++)
            {            
                if((input[i].type='checkbox')&&(input[i].id!=event.srcElement.id))
                    document.getElementById(input[i].id).checked=false;
            }       
        }
        </script><asp:GridView ID="gv" runat="server">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        
        <asp:CheckBox ID="ckbOne" Text="合格"  runat="server"  onclick="singleChecked();" />
        <asp:CheckBox ID="ckbTwo" Text="不合格" runat="server" onclick="singleChecked();" />
        <asp:CheckBox ID="ckbThree" Text="优秀" runat="server" onclick="singleChecked();" />    </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
      

  8.   

    你好cutbug,  你的方法整个datagrid只能输入一个合格或者不合格,我的要求是没条记录 可以为 合格、不合格、不填写
      

  9.   

    这个是每行里面一个checkboxlist的代码,测试过的
      

  10.   

    CutBug ,不行呀,我测试怎么通不过呀aspx页面<html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
        <script language="javascript">
        function eforCheck(gridID,checkListID){
            var obj = null;
            var grid = document.getElementById(gridID);
            if(grid==null) return;
            var tables = grid.getElementsByTagName("table");
            for(var i=0;i<tables.length;i++){
                if(tables[i].id.indexOf(checkListID)!=-1){
                    var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
                    for(var j=0;j<chks.length;j++){
                        chks[j].onclick=function(){
                            if(this.checked){
                                if(obj!=null && obj!=this)obj.checked=false;
                                obj = this;
                            }
                        };
                    }
                }
            }
        }
       
        window.onload=function(){
            eforCheck("<%=DG_Data.ClientID%>","ItemCheckResult");
        };
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:DataGrid ID="DG_Data" runat="server"> 
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                
                <input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
                
                    <asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" 
                                            Height="1px" RepeatDirection="Vertical" > 
                            <asp:ListItem>合格 </asp:ListItem> 
                            <asp:ListItem>不合格 </asp:ListItem> 
                            <asp:ListItem>优秀 </asp:ListItem> 
                     </asp:CheckBoxList> 
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
        </asp:DataGrid>                    
        </form>
    </body>
    </html>cs代码
    using System.Text;
    using System.Data.SqlClient;public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //string connectionString = "server = (192.168.0.199);database = pratice;user id = sa;pwd = "; //dbconn.GetConnPara();            string connectionString = "server=192.168.0.199;database=XuanwuSupervision;uid=sa;pwd=sa";             SqlConnection myConnection = new SqlConnection(connectionString);
                SqlDataAdapter myAdapter = new SqlDataAdapter();
                SqlCommand myCommand = new SqlCommand();
                myCommand.CommandText = "select CheckCode,CheckItem,CheckContent,IsKeyProject,Re from DMBusinessCheck";            myCommand.Connection = myConnection;
                myAdapter.SelectCommand = myCommand;            DataTable choosedt = new DataTable();            myAdapter.Fill(choosedt);            DG_Data.DataSource = choosedt;// fillDT();
                DG_Data.DataBind();
                
            }
        }
    }
      

  11.   

    下面是我的完整测试数据,IE6.0没有问题,你自己新建一个页面把代码拷进去看看
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
        <script language="javascript">
        function eforCheck(gridID,checkListID){
            var obj = null;
            var grid = document.getElementById(gridID);
            if(grid==null) return;
            var tables = grid.getElementsByTagName("table");
            for(var i=0;i<tables.length;i++){
                if(tables[i].id.indexOf(checkListID)!=-1){
                    var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
                    for(var j=0;j<chks.length;j++){
                        chks[j].onclick=function(){
                            if(this.checked){
                                if(obj!=null && obj!=this)obj.checked=false;
                                obj = this;
                            }
                        };
                    }
                }
            }
        }
       
        window.onload=function(){
            eforCheck("<%=DG_Data.ClientID%>","ItemCheckResult");
        };
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:DataGrid ID="DG_Data" runat="server"> 
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                     <input id="SelectedID" runat="server" type="hidden" />
                    <asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" 
                                            Height="1px" RepeatDirection="Vertical" > 
                            <asp:ListItem>合格 </asp:ListItem> 
                            <asp:ListItem>不合格 </asp:ListItem> 
                            <asp:ListItem>优秀 </asp:ListItem> 
                     </asp:CheckBoxList> 
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
        </asp:DataGrid>                    
        </form>
    </body>
    </html> protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("a", typeof(string)));
            dt.Rows.Add(1);
            dt.Rows.Add(2);
            dt.Rows.Add(3);
            DG_Data.DataSource = dt;
            DG_Data.DataBind();
        }
      

  12.   

    CutBug  你好13楼的代码还是整个datagrid只能只能选择一个“合格、不合格、优秀”,我要求的是每一行数据中最多只能选择一个“合格、不合格、优秀” ,我的是 。net 2005 ,ie6
      

  13.   

    这个函数改一下
    function eforCheck(gridID,checkListID){
            var objs = {};
            var grid = document.getElementById(gridID);
            if(grid==null) return;
            var tables = grid.getElementsByTagName("table");
            for(var i=0;i<tables.length;i++){
                if(tables[i].id.indexOf(checkListID)!=-1){
                    objs[tables[i].id] = null;
                    var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
                    for(var j=0;j<chks.length;j++){
                        chks[j].onclick=function(){
                            if(this.checked){
                                var pid = this.parentNode.parentNode.parentNode.parentNode.id;
                                if(objs[pid]!=null && objs[pid]!=this){
                                    objs[pid].checked=false;
                                    objs[pid] = null;
                                }
                                objs[pid] = this;
                            }
                        };
                    }
                }
            }
        }
      

  14.   


    var pid = this.parentNode.parentNode.parentNode.parentNode.id;一定要这样表示?
      

  15.   

    谢谢二位!CutBug ,我先还有个小问题,15楼的方法可以了,但是进入该页面有2个口,一个新建、一个修改,前者可以,后者修改(预先选择了部分合格or不合格),对于预先选择了的,可以同时选择合格and不合格,也就是系统没有看到预先选择的。不知我说清楚没有?
    谢谢!
      

  16.   


    <script>
        function singleChecked(obj)
        {             
            var input=obj.getElementsByTagName('input');                      
                
            for(var i=0;i<input.length;i++)
            {            
                if((input[i].type='checkbox')&&(input[i].id!=event.srcElement.id))
                    document.getElementById(input[i].id).checked=false;
            }       
        }
        function addClickEvent(id,colIndex)
        {
            var table=document.getElementById(id);
            if(table)
            {    
                var tempID;     
                for(var i=1;i<table.rows.length;i++)
                {
                    var input=table.rows[i].cells[colIndex].getElementsByTagName('input');                                               
                    tempID=table.rows[i].cells[colIndex].childNodes[0].id;
                     
                    for(var j=0;j<input.length;j++)
                    {
                        if(input[j].type='checkbox')
                        {                                                                                                                                             
                            (function(){
                                var tb=document.getElementById(tempID);//每个闭包使用不同的变量                            
                                input[j].onclick = function(){singleChecked(tb);};
                            })();                                                                                                                                  
                        }
                    }
                   
                }
            }
            else
                return;
        }
    </script>
    使用的时候<script>
        addClickEvent('<%=gv.ClientID %>',0);
    </script>把这段放在gridview的html代码下面 就行, gv 是girdview 的ID ,0 是表示 gridview 的第一列
    有一个地方需要注意一下

    tempID=table.rows[i].cells[colIndex].childNodes[0].id;

    childNodes[0] 中的 0 是硬编码,意思是获取td的第一个子元素.你可以根据需要改成参数配置的形式,像列现在的写法就可以了。
      

  17.   

    谢谢!18楼的问题可以解决么?我不太明白这句话: 把这段放在gridview的html代码下面 就行, gv 是girdview 的ID ,0 是表示 gridview 的第一列 
    有一个地方需要注意一下 <asp:DataGrid ID="DG_Data" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
                            CellPadding="3" Font-Size="10pt" GridLines="Vertical" PageSize="1000" Width="780px" >
                            
                            
                            
                            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" Mode="NumericPages" />
                            <AlternatingItemStyle BackColor="Gainsboro" />
                            <ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
                            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                            <Columns>
                                
                                <asp:BoundColumn DataField="CheckCode" HeaderText="编号"></asp:BoundColumn>
                                <asp:BoundColumn DataField="CheckItem" HeaderText="检查项目">
                                    <ItemStyle Width="80px" />
                                </asp:BoundColumn>
                                               
                                
                                <asp:TemplateColumn HeaderText="结  果" ItemStyle-Width = "70">
                                    <ItemTemplate>
                                        <input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
                                            
                                        <asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" Height="1px" RepeatDirection="Vertical" > 
                                            <asp:ListItem>合格</asp:ListItem> 
                                            <asp:ListItem>不合格</asp:ListItem>                                        
                                        </asp:CheckBoxList>                                                                       
                                        
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                </asp:TemplateColumn>     
                        
                                        
                            </Columns>
                        </asp:DataGrid>
      

  18.   

    <Columns> 
                                
                                <asp:BoundColumn DataField="CheckCode" HeaderText="编号"> </asp:BoundColumn> 
                                <asp:BoundColumn DataField="CheckItem" HeaderText="检查项目"> 
                                    <ItemStyle Width="80px" /> 
                                </asp:BoundColumn> 
                                              
                                
                                <asp:TemplateColumn HeaderText="结  果" ItemStyle-Width = "70"> 
                                    <ItemTemplate> 
                                        <input id="SelectedID" runat="server" type="hidden" value=' <%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' /> 
                                            
                                        <asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" Height="1px" RepeatDirection="Vertical" > 
                                            <asp:ListItem>合格 </asp:ListItem> 
                                            <asp:ListItem>不合格 </asp:ListItem>                                        
                                        </asp:CheckBoxList>                                                                      
                                        
                                    </ItemTemplate> 
                                    <HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" /> 
                                </asp:TemplateColumn>    
                        
                                        
                            </Columns> 你这个Columns 中 就包含了3列,那列号就为2
    你的DataGrid 的  id为DG_Data
    你把<script>
        addClickEvent('<%=DG_Data.ClientID %>',0);
    </script>放在DataGrid 的下面 就可以了