c# gridview checkbox实现单选

关于gridview添加checkbox后,如何使用js添加单选事件,其实代码不是很难,不过当初也查了会资料,仅供记录,高手绕道哈。。aspx中格式如下:

<div>
    <table>
        <td><asp:GridView ID="HumIdNmGridView">
            <Columns>
               <asp:TemplateField> 
                     <HeaderTemplate>
                         <asp:Image ID="oImg" runat="server" ImageUrl="../../images/icon_sure.gif">
                         </asp:Image> //图标可以忽略
                       </HeaderTemplate>
                         <ItemTemplate>
                             <asp:CheckBox ID="chkSelected" runat="server" 
                             onclick="SingleCheck(‘HumIdNmGridView‘,this)"></asp:CheckBox>
                         </ItemTemplate>
            </td>
    </table>
</div>

下面是javascript:

var ClickCount = 0; //判断同一checkbox连续点击次数
        var PreCheckboxId = ""; //记录点击checkbox的ID
        //gridviewId 为传入的gridview的Id
        function SingleCheck(DivGridView, SelectCheckBox) {
            //console.info(DivGridView);
            //console.info(SelectCheckBox);
            //判断是否连续点击同一个checkbox
            if (PreCheckboxId == SelectCheckBox) {
                ClickCount += 1;
            } else {
                PreCheckboxId = SelectCheckBox.id;
                ClickCount = 0;
            }
            //找到包含checkbox的divId
            var GridViewList = document.getElementById(DivGridView);
            //获取divId中的多有input
            var objs = GridViewList.getElementsByTagName("input");
            for (var i = 0; i < objs.length; i++) {
                if (objs[i].type.toLowerCase() == "checkbox") {
                    objs[i].checked = false;
                }

                var SelectCheckBoxId = SelectCheckBox.id;
                //如果点击的checkbox次数为奇数则checkbox为选中,反之不选中
                if (ClickCount % 2 != 1) {
                    document.getElementById(SelectCheckBoxId).checked = true;
                } else {
                    document.getElementById(SelectCheckBoxId).checked = false;
                }
            }
        }



c# gridview checkbox实现单选

上一篇:AutoreleasePool源码学习


下一篇:解决window.open传值的问题