读书人

动态绑定的asp.checkboxlist control

发布时间: 2012-11-05 09:35:12 作者: rapoo

动态绑定的asp.checkboxlist control client side 判断是否selected
一个 checkbox,text = all and autopostback= true. 一个checkboxlist(动态绑定),autopostback= false。
需求
1.check checkbox, 选中所有checkboxlist 的item. 这个比较好实现.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
foreach (ListItem item in CheckBoxList1.Items)
item.Selected = (sender as CheckBox).Checked;
}

2.如果checkbox和所有checkboxlist item是选中状态,uncheck一个 checkgoxlist item,checkbox 的选中状态改变成unchecked.

因为checkboxlist autopostback =false, 我想是应该用javascript实现,如何实现呢?

[解决办法]
居然是半夜发的。。。

给ListItem添加客户端(js)的点击(选中)事件,,,然后再js中找到那个text=all的Checkbox,控制它的选中状态。。。

其实这种checkbox用name来区别比较好控制一点。。。
[解决办法]

HTML code
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script src="Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>    <script type="text/javascript">        $(function () {            $("#chkAll").click(function () {                $("#chkList input[type='checkbox']").prop("checked", $(this).prop("checked"));            });        });    </script></head><body>    <form id="form1" runat="server">    <div>        <asp:CheckBox ID="chkAll" Text="All" runat="server" />        <asp:CheckBoxList ID="chkList" runat="server">            <asp:ListItem Text="text1" />            <asp:ListItem Text="text2" />        </asp:CheckBoxList>    </div>    </form></body></html> 

读书人网 >JavaScript

热点推荐