陋室铭
永远也不要停下学习的脚步(大道至简至易)

在后台GridView_RowDataBound事件中
CheckBox cbx = e.Row.FindControl("cbxName") as CheckBox;
        try
        {
            //绑定选中CheckBox 客户端ID
            cbx.Attributes.Add("onclick", "ChangeGet(" + cbx.ClientID + ")");
        }
        catch
        {
        }
前台js脚本代码
<script language="javascript">
function ChangeGet(SelectCheckBox)
{            
//找到页面所有 input
  var objs = document.getElementsByTagName("input");
  for(var i=0; i<objs.length; i++)
    {
//找到input中的checkbox
        if(objs[i].type.toLowerCase() == "checkbox" )
//所有checkbox为false
        objs[i].checked = false;
    }
//找到选中checkbox
   var SelectCheckBoxID=SelectCheckBox.id;
//选中checkbox为true
   document.getElementById(SelectCheckBoxID).checked = true;
}

</script>





或者:

在后台GridView_RowDataBound事件中
CheckBox cbx = e.Row.FindControl("cbxName") as CheckBox;
        try
        {
            //绑定选中CheckBox 客户端ID
            cbx.Attributes.Add("onclick", "ChangeGet(this)");
        }
        catch
        {
        }
前台js脚本代码
<script language="javascript">
function ChangeGet(SelectCheckBox)
{            
//找到页面所有 input
  var objs = document.getElementsByTagName("input");
  for(var i=0; i<objs.length; i++)
    {
//找到input中的checkbox
        if(objs[i].type.toLowerCase() == "checkbox" )
//所有checkbox为false
        objs[i].checked = false;
    }
//选中checkbox为true
   SelectCheckBoxID.checked = true;
}

</script>


写个完善一点的 你看看 

    <script language="javascript">

       
       
var ClickNum=0;//判断同一checkbox连续点击次数
var PreCheckboxID="";//记录点击checkbox的ID
//listId为控制checkbox的范围
//SelectRadio是点击checkbox自己
function SelectRadio(listId,SelectRadio)
{
    ///判断是否连击同一个checkbox
    if(PreCheckboxID==SelectRadio.id)
    {
        ClickNum = ClickNum + 1;
    }
    else
    {
        PreCheckboxID = SelectRadio.id;
        ClickNum = 0;
    }
    //找到控制范围
    var GridViewtableSearchList = document.getElementById(listId);
    //找到控制范围下所有input
    var objs = GridViewtableSearchList.getElementsByTagName("input");
    //找到控制范围下所有checkbox并都变为false
    for(var i = 0; i < objs.length; i++)
    {
        if(objs[i].type.toLowerCase() == "checkbox" )
        objs[i].checked = false;
    }
    var SelectRadioID = SelectRadio.id;
    //如果连击次数为奇次checked为true偶次为false
    if(ClickNum%2!=1)
    {
        document.getElementById(SelectRadioID).checked = true;
    }
    else
    {
        document.getElementById(SelectRadioID).checked = false;
    }
}

    </script>




    <form id="form1" runat="server">
        <div id="DivGridview">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="a"
                DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="a" HeaderText="a" InsertVisible="False" ReadOnly="True"
                        SortExpression="a" />
                    <asp:BoundField DataField="b" HeaderText="b" SortExpression="b" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" onclick="SelectRadio('DivGridview',this)"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>

    </form>

posted on 2007-01-31 15:22  宏宇  阅读(4415)  评论(6编辑  收藏  举报