当前位置:编程学习 > C#/ASP.NET >>

checkbox让checkboxlist全选的问题

在.net里面有没什么好的方法???好象要JS才能实现,能给个完全的JS代码吗?急急急...有.net方法最好 --------------------编程问答-------------------- 在 Checkbox 点击事件里写个循环,把Checkboxlist 里的每一个都Check一次就OK了 --------------------编程问答-------------------- http://www.cnblogs.com/lovecherry/archive/2005/05/15/155835.html

其实代码你可以自己搜索得到,懒惰啊懒惰 --------------------编程问答-------------------- <script>
function SelectAll(objID){
 var checkBoxList = document.getElementById(objID);
 for(var i = 0 ;i<checkBoxList.length;i++){
   checkBoxList[i].checked = true;
 }
}
</script> --------------------编程问答-------------------- 好象要JS才能实现?
用后台代码更容易
foreach(ListItem i in checkboxlist.items)
i.Selected = true; --------------------编程问答-------------------- try these codes as below:

<%@ Page Language="C#" AutoEventWireup="true" %>

<%-- http://community.csdn.net/Expert/TopicView3.asp?id=5602681 --%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    function doActionForCheckBoxListItem(sender, chkListId) 
    {
        var chkListContainer = document.getElementById(chkListId);
        var chkList = chkListContainer.getElementsByTagName("input");        
        for(var i=0; i<chkList.length;i++) {            
            chkList[i].checked = sender.checked;
            // more operations
            // ...
        }             
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox ID="CheckBox1" Text="Check All" runat="server" onclick="doActionForCheckBoxListItem(this, 'CheckBoxList1')" />
        <asp:CheckBoxList ID="CheckBoxList1" BorderWidth="1" runat="server">
        <asp:ListItem Value="Item1">Item1</asp:ListItem>
        <asp:ListItem Value="Item2">Item2</asp:ListItem>
        <asp:ListItem Value="Item3">Item3</asp:ListItem>
        <asp:ListItem Value="Item4">Item4</asp:ListItem>
        </asp:CheckBoxList>      
    </div>
    </form>
</body>
</html>

Good Luck! --------------------编程问答-------------------- 用JS不用postback好一些. --------------------编程问答-------------------- //aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
    function ChooseAll()
    {
    var inputs = document.all.tags("INPUT");
    for (var i=0; i < inputs.length; i++) // 遍历页面上所有的 input 
    {
    if (inputs[i].type == "checkbox" && inputs[i].value != "CheckBox1" )
    {
    inputs[i].checked = document.getElementById("CheckBox1").checked;
    }     
    }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem Value="3"></asp:ListItem>
        </asp:CheckBoxList>
    </form>
</body>
</html>

//aspx.cs
    protected void Page_Load(object sender, System.EventArgs e)
    {
        CheckBox1.Attributes.Add("onclick", "ChooseAll();");
    } --------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- 好。。。。。。。。。。。。。。。。。 --------------------编程问答-------------------- 这种代码应该自己尝试着去写。。。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
    function checkedAll(){
        var container=document.getElementById("<%= CheckBoxList1.ClientID%>");
        var cbList=container.getElementsByTagName("input");
        for(var i=0;i<cbList.length;i++){
            if(cbList[i].type=="checkbox"){
                cbList[i].checked=true;
            }
        }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Value="1" Text="CheckBox1"></asp:ListItem>
            <asp:ListItem Value="2" Text="CheckBox2"></asp:ListItem>
            <asp:ListItem Value="3" Text="CheckBox3"></asp:ListItem>
            <asp:ListItem Value="4" Text="CheckBox4"></asp:ListItem>
        </asp:CheckBoxList>
        <input type="button" value="JS全选" onclick="checkedAll()" />
        <asp:Button ID="Button2" runat="server" Text="后台全选" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>




public partial class CheckedAll : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in this.CheckBoxList1.Items)
            {
                item.Selected=true;
            }
        }
    }
--------------------编程问答-------------------- 07年的帖子LZ早是牛人了吧






--------------------编程问答--------------------
引用 11 楼 wxr0323 的回复:
07年的帖子LZ早是牛人了吧

哈哈~~   才发现时07年的
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,