日期:2014-05-17  浏览次数:20634 次

JS DataList CheckBox与CheckBoxList联动。
代码如下:
<asp:DataList ID=dlGroup runat=server onitemdatabound="dlGroup_ItemDataBound">
  <ItemTemplate>
  <asp:CheckBox ID=ckGroup runat=server Text='<%# DataBinder.Eval(Container.DataItem,"GroupName") %>' onclick= JS方法/>
  <asp:HiddenField ID=hidGroupTimePK runat=server Value='<%# DataBinder.Eval(Container.DataItem,"TimePK") %>' />
  <asp:CheckBoxList ID=ckPsn runat=server RepeatColumns=8></asp:CheckBoxList>
  </ItemTemplate>
</asp:DataList>

我的想法是通过JS 选择CheckBox时,和它一级的 CheckBoxList全选择,否则取消。

------解决方案--------------------

protected void dlGroup_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
CheckBox ckGroup = e.Item.FindControl("ckGroup") as CheckBox;
CheckBoxList ckPsn = e.Item.FindControl("ckPsn") as CheckBoxList;
ckGroup.Attributes.Add("onclick", "SetNext(this,'" + ckPsn.ClientID + "')");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function SetNext(o,xid) {
inputs = document.getElementById(xid).getElementsByTagName("input");
for (i = 0; i < inputs.length; i++) {
inputs[i].checked = o.checked;
}
}
</script>
</head>