首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

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

2012-05-01 
JS DataList CheckBox与CheckBoxList联动。代码如下:asp:DataList IDdlGroup runatserver onitemdatabou

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>

热点排行