当前位置: 代码迷 >> ASP.NET >> JS DataList CheckBox与CheckBoxList联动。解决思路
  详细解决方案

JS DataList CheckBox与CheckBoxList联动。解决思路

热度:8982   发布时间:2013-02-25 00:00:00.0
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>
  相关解决方案