GridView中CheckBox的用法

以下是我在GridView中控制CheckBox的全选/取消全选,根据子CheckBox处理全选CheckBox状态的操作并获取所选GridView中所选行的列的值。
脚本代码:
    <script language="javascript" type="text/javascript">
    //全选/全取消GridView中的CheckBox
    //参数gv:  GridView的ID
    //参数input2: 功能控件ID(全选CheckBox)
    function CheckAll(gv,input2)
    {
        var objForm = document.getElementById(gv);
        var objLen = objForm.rows.length;
        for (var iCount = 0; iCount < objLen; iCount++)
        {
             objForm.rows[iCount].cells[0].getElementsByTagName("INPUT")[0].checked = input2.checked;
        }
    }
       
    function CheckSelect(gv,input2)
    {
        if (input2.checked == true)
        {
            if(GetCheckBoxCount(gv) == GetCheckedBoxCount(gv))
            {
                form1.Checkbox3.checked = true;
            }
        }
        else
        {
            form1.Checkbox3.checked = false;
        }
    }
   
    function GetCheckBoxCount(gv)
    {
        var objForm = document.getElementById(gv);
        var objLen = objForm.rows.length;
        return (objLen-1);
    }
   
    function GetCheckedBoxCount(gv)
    {
        var count;
        count = 0;
        var objForm = document.getElementById(gv);
        var objLen = objForm.rows.length;
        for(var iCount = 0; iCount < objLen; iCount++)
        {
             if(objForm.rows[iCount].cells[0].getElementsByTagName("INPUT")[0].checked == true)
             {
                 count ++;
             }
        }
        return count;
    }   
    </script>
页面代码:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="LinqDataSource1" DataKeyNames="CarID">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox2" runat="server" Width="20px" onclick="CheckSelect('GridView1',this)"/>
                    </ItemTemplate>
                    <HeaderTemplate>
                        <input ID="Checkbox3" type="checkbox" style="width:20px" onclick="CheckAll('GridView1',this)"/>
                    </HeaderTemplate>
                    <ItemStyle Width="20px" />
                </asp:TemplateField>
                <asp:BoundField DataField="CarID" HeaderText="CarID" ReadOnly="True"
                    SortExpression="CarID" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />

        <asp:LinqDataSource ID="LinqDataSource1" runat="server"
            ContextTypeName="DBML.CarDataContext" Select="new (CarID, TeamID, DriverID)"
            TableName="CarInfo">
        </asp:LinqDataSource>    
    </div>
    </form>
< /body>

cs文件代码:
 protected void Button1_Click(object sender, EventArgs e)
        {
            int count = 0;
            string items = "";
            foreach (GridViewRow item in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)item.FindControl("CheckBox2");
                if (cb.Checked)
                {
                    items = items + item.Cells[1].Text;//已选行的第2列的值
                    count++;
                }
            }
            Response.Write(count.ToString() + items);
        }

以上代码可能通用性还有待增强,也希望对大家有所帮助。谢谢!

posted on 2013-11-23 10:45  程序猴chengxuhou.com  阅读(1395)  评论(0编辑  收藏  举报

导航