CheckBox联动问题 大家好,请问如何实现CheckBox与CheckBoxList的联动问题,如果选中CheckBox,则CheckBoxList全部选中;取消CheckBox选择,则CheckBoxList全部取消选择。如果选中CheckBoxList其中一项,则CheckBox选中;CheckBoxList全部取消,则CheckBox取消选择。 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为CheckBox添加个JS脚本事件就可以了 简单点的就是用JQ实现,看代码http://www.cnblogs.com/libingql/archive/2011/11/07/2238663.html <script type="text/C#" language="javascript"> function AddRemoveValues(oChk) { if(oChk.checked)Form1.HdnSelectedValues.value += "," + oChk.value; elseForm1.HdnSelectedValues.value = Form1.HdnSelectedValues.value.replace("," + oChk.value,""); } </script><form id="Form1" method="post" runat="server"><asp:CheckBox ID="checkedAll" runat="server" AutoPostBack="True" OnCheckedChanged="checkedAll_CheckedChanged" /><asp:Label ID="Label7" runat="server">全选</asp:Label><asp:DataList ID="dl1" runat="server" RepeatColumns="1" Width="100%" OnItemDataBound="dl1_ItemDataBound"> <ItemTemplate> <tr> <td align="center" valign="top"> <table id="Table2" border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td> </td> <td align="left"> <%# DataBinder.Eval(Container.DataItem, "字段1")%> </td> </tr> </table> <table id="Table3"> <tr> <td valign="top"> <table id="Table5" border="0"> <tr> <td align="center"> <asp:DataList ID="dl2" runat="server" RepeatColumns="3" Width="100%"> <ItemTemplate> <input id="chkSelect" runat="server" name="chkSelect" onclick="AddRemoveValues(this)" type="checkbox" value='<%# DataBinder.Eval(Container.DataItem, "字段2")%>' /> <%# GetMethod(DataBinder.Eval(Container.DataItem, "字段1").ToString(), DataBinder.Eval(Container.DataItem, "字段2").ToString())%> </ItemTemplate> </asp:DataList> </td> </tr> </table> </td> </tr> </table> </td> </tr> </ItemTemplate> </asp:DataList></form>public void dl1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataList dataList = (DataList)e.Item.FindControl("dl2"); for (int j = 0; j < dataList.Items.Count; j++) { HtmlInputCheckBox chkbox = dataList.Items[j].FindControl("chkSelect") as HtmlInputCheckBox; } DataRowView rowv = (DataRowView)e.Item.DataItem; string str字段1= Convert.ToString(rowv["字段1"]); string strSql = "要写的sql语句"; DataTable mytable = rdtable(strSql); string strId7 = mytable.Rows[0]["Id"].ToString(); string strId7 = ""; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { strId7+= CheckBoxList1.Items[i].Value + ","; } } if (strId7 != "") { strId7=strId7.Substring(0, strId7.Length - 1); } string sql = "要写的sql语句"; DataTable dt = rdtable(sql); if (dt != null) { try { dataList.DataSource = dt; dataList.DataBind(); } catch (Exception ex) { throw new Exception(ex.Message); } } } }public void checkedAll_CheckedChanged(object sender, System.EventArgs e) { // 选中全选 if (checkedAll.Checked.Equals(true)) { int i = 0; for (i = 0; i < dl1.Items.Count; i++) { DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList; if (dl2 != null) { for (int j = 0; j < dl2.Items.Count; j++) { HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox; // 选中 chkbox.Checked = true; } } } Label5.Text = "全不选"; } // 取消全选 if (checkedAll.Checked.Equals(false)) { int i = 0; for (i = 0; i < dl1.Items.Count; i++) { DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList; if (dl2 != null) { for (int j = 0; j < dl2.Items.Count; j++) { HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox; // 选中 chkbox.Checked = false; } } } Label5.Text = "全选"; } } 参考这个例子。<script type="text/C#" language="javascript"> function AddRemoveValues(oChk) { if(oChk.checked)Form1.HdnSelectedValues.value += "," + oChk.value; elseForm1.HdnSelectedValues.value = Form1.HdnSelectedValues.value.replace("," + oChk.value,""); } </script><form id="Form1" method="post" runat="server"><asp:CheckBox ID="checkedAll" runat="server" AutoPostBack="True" OnCheckedChanged="checkedAll_CheckedChanged" /><asp:Label ID="Label7" runat="server">全选</asp:Label><asp:DataList ID="dl1" runat="server" RepeatColumns="1" Width="100%" OnItemDataBound="dl1_ItemDataBound"> <ItemTemplate> <tr> <td align="center" valign="top"> <table id="Table2" border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td> </td> <td align="left"> <%# DataBinder.Eval(Container.DataItem, "字段1")%> </td> </tr> </table> <table id="Table3"> <tr> <td valign="top"> <table id="Table5" border="0"> <tr> <td align="center"> <asp:DataList ID="dl2" runat="server" RepeatColumns="3" Width="100%"> <ItemTemplate> <input id="chkSelect" runat="server" name="chkSelect" onclick="AddRemoveValues(this)" type="checkbox" value='<%# DataBinder.Eval(Container.DataItem, "字段2")%>' /> <%# GetMethod(DataBinder.Eval(Container.DataItem, "字段1").ToString(), DataBinder.Eval(Container.DataItem, "字段2").ToString())%> </ItemTemplate> </asp:DataList> </td> </tr> </table> </td> </tr> </table> </td> </tr> </ItemTemplate> </asp:DataList></form>public void dl1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataList dataList = (DataList)e.Item.FindControl("dl2"); for (int j = 0; j < dataList.Items.Count; j++) { HtmlInputCheckBox chkbox = dataList.Items[j].FindControl("chkSelect") as HtmlInputCheckBox; } DataRowView rowv = (DataRowView)e.Item.DataItem; string str字段1= Convert.ToString(rowv["字段1"]); string strSql = "要写的sql语句"; DataTable mytable = rdtable(strSql); string strId7 = mytable.Rows[0]["Id"].ToString(); string strId7 = ""; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { strId7+= CheckBoxList1.Items[i].Value + ","; } } if (strId7 != "") { strId7=strId7.Substring(0, strId7.Length - 1); } string sql = "要写的sql语句"; DataTable dt = rdtable(sql); if (dt != null) { try { dataList.DataSource = dt; dataList.DataBind(); } catch (Exception ex) { throw new Exception(ex.Message); } } } }public void checkedAll_CheckedChanged(object sender, System.EventArgs e) { // 选中全选 if (checkedAll.Checked.Equals(true)) { int i = 0; for (i = 0; i < dl1.Items.Count; i++) { DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList; if (dl2 != null) { for (int j = 0; j < dl2.Items.Count; j++) { HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox; // 选中 chkbox.Checked = true; } } } Label5.Text = "全不选"; } // 取消全选 if (checkedAll.Checked.Equals(false)) { int i = 0; for (i = 0; i < dl1.Items.Count; i++) { DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList; if (dl2 != null) { for (int j = 0; j < dl2.Items.Count; j++) { HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox; // 选中 chkbox.Checked = false; } } } Label5.Text = "全选"; } } 直接用JQuery就行了吧 jquery增加 全选按钮的 checkbox的click事件 根据 Class属性,获取整个checkbox组,遍历选中 测试代码<body><script src="Scripts/jquery-1.7.1.js"></script> <form> <input type="checkbox" id="cboMain" class="cbo"/> <input type="checkbox" class="cbo"/> <input type="checkbox" class="cbo"/> <input type="checkbox" class="cbo"/> <input type="checkbox" class="cbo"/> </form></body> <script> $('#cboMain').click(function () { var result = $(this).is(':checked'); alert(result); $('.cbo').attr("checked", result); }) </script> 如何消除ifeame的滚动条 越活越回去了,被一个delete语句蒙住 GridView嵌套RadioButtonList的绑定问题 特殊的事件绑定 服务器生成Word文档后想立即发给客户端,如何实现? 请解释如下代码为什么不能通过,onclick和onserverclick 请问用脚本如何让界面中的控件中的内容清空? 为什么我现在不能回帖呢 请问ViewState[abc.Tostring()]什么作用呢? 一个项目需要的综合定制,大量分数!请参与讨论! 全局变量赋值问题 抓取页面遇到url编码的问题
http://www.cnblogs.com/libingql/archive/2011/11/07/2238663.html
function AddRemoveValues(oChk) {
if(oChk.checked)
Form1.HdnSelectedValues.value += "," + oChk.value;
else
Form1.HdnSelectedValues.value = Form1.HdnSelectedValues.value.replace("," + oChk.value,"");
}
</script><form id="Form1" method="post" runat="server"><asp:CheckBox ID="checkedAll" runat="server" AutoPostBack="True" OnCheckedChanged="checkedAll_CheckedChanged" /><asp:Label
ID="Label7" runat="server">全选</asp:Label><asp:DataList ID="dl1" runat="server" RepeatColumns="1" Width="100%" OnItemDataBound="dl1_ItemDataBound">
<ItemTemplate>
<tr>
<td align="center" valign="top">
<table id="Table2" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
</td>
<td align="left">
<%# DataBinder.Eval(Container.DataItem, "字段1")%>
</td>
</tr>
</table>
<table id="Table3">
<tr>
<td valign="top">
<table id="Table5" border="0">
<tr>
<td align="center">
<asp:DataList ID="dl2" runat="server" RepeatColumns="3" Width="100%">
<ItemTemplate>
<input id="chkSelect" runat="server" name="chkSelect" onclick="AddRemoveValues(this)"
type="checkbox" value='<%# DataBinder.Eval(Container.DataItem, "字段2")%>' />
<%# GetMethod(DataBinder.Eval(Container.DataItem, "字段1").ToString(), DataBinder.Eval(Container.DataItem, "字段2").ToString())%>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
</form>
public void dl1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dataList = (DataList)e.Item.FindControl("dl2"); for (int j = 0; j < dataList.Items.Count; j++)
{
HtmlInputCheckBox chkbox = dataList.Items[j].FindControl("chkSelect") as HtmlInputCheckBox;
}
DataRowView rowv = (DataRowView)e.Item.DataItem;
string str字段1= Convert.ToString(rowv["字段1"]);
string strSql = "要写的sql语句";
DataTable mytable = rdtable(strSql);
string strId7 = mytable.Rows[0]["Id"].ToString();
string strId7 = ""; for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
strId7+= CheckBoxList1.Items[i].Value + ",";
}
}
if (strId7 != "")
{
strId7=strId7.Substring(0, strId7.Length - 1); }
string sql = "要写的sql语句";
DataTable dt = rdtable(sql); if (dt != null)
{
try
{
dataList.DataSource = dt;
dataList.DataBind();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
public void checkedAll_CheckedChanged(object sender, System.EventArgs e)
{
// 选中全选
if (checkedAll.Checked.Equals(true))
{
int i = 0;
for (i = 0; i < dl1.Items.Count; i++)
{
DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList;
if (dl2 != null)
{
for (int j = 0; j < dl2.Items.Count; j++)
{
HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox;
// 选中
chkbox.Checked = true;
}
}
}
Label5.Text = "全不选"; }
// 取消全选
if (checkedAll.Checked.Equals(false))
{
int i = 0;
for (i = 0; i < dl1.Items.Count; i++)
{
DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList;
if (dl2 != null)
{
for (int j = 0; j < dl2.Items.Count; j++)
{
HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox;
// 选中
chkbox.Checked = false;
}
}
}
Label5.Text = "全选"; }
}
function AddRemoveValues(oChk) {
if(oChk.checked)
Form1.HdnSelectedValues.value += "," + oChk.value;
else
Form1.HdnSelectedValues.value = Form1.HdnSelectedValues.value.replace("," + oChk.value,"");
}
</script><form id="Form1" method="post" runat="server"><asp:CheckBox ID="checkedAll" runat="server" AutoPostBack="True" OnCheckedChanged="checkedAll_CheckedChanged" /><asp:Label
ID="Label7" runat="server">全选</asp:Label><asp:DataList ID="dl1" runat="server" RepeatColumns="1" Width="100%" OnItemDataBound="dl1_ItemDataBound">
<ItemTemplate>
<tr>
<td align="center" valign="top">
<table id="Table2" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
</td>
<td align="left">
<%# DataBinder.Eval(Container.DataItem, "字段1")%>
</td>
</tr>
</table>
<table id="Table3">
<tr>
<td valign="top">
<table id="Table5" border="0">
<tr>
<td align="center">
<asp:DataList ID="dl2" runat="server" RepeatColumns="3" Width="100%">
<ItemTemplate>
<input id="chkSelect" runat="server" name="chkSelect" onclick="AddRemoveValues(this)"
type="checkbox" value='<%# DataBinder.Eval(Container.DataItem, "字段2")%>' />
<%# GetMethod(DataBinder.Eval(Container.DataItem, "字段1").ToString(), DataBinder.Eval(Container.DataItem, "字段2").ToString())%>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
</form>
public void dl1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dataList = (DataList)e.Item.FindControl("dl2"); for (int j = 0; j < dataList.Items.Count; j++)
{
HtmlInputCheckBox chkbox = dataList.Items[j].FindControl("chkSelect") as HtmlInputCheckBox;
}
DataRowView rowv = (DataRowView)e.Item.DataItem;
string str字段1= Convert.ToString(rowv["字段1"]);
string strSql = "要写的sql语句";
DataTable mytable = rdtable(strSql);
string strId7 = mytable.Rows[0]["Id"].ToString();
string strId7 = ""; for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
strId7+= CheckBoxList1.Items[i].Value + ",";
}
}
if (strId7 != "")
{
strId7=strId7.Substring(0, strId7.Length - 1); }
string sql = "要写的sql语句";
DataTable dt = rdtable(sql); if (dt != null)
{
try
{
dataList.DataSource = dt;
dataList.DataBind();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
public void checkedAll_CheckedChanged(object sender, System.EventArgs e)
{
// 选中全选
if (checkedAll.Checked.Equals(true))
{
int i = 0;
for (i = 0; i < dl1.Items.Count; i++)
{
DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList;
if (dl2 != null)
{
for (int j = 0; j < dl2.Items.Count; j++)
{
HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox;
// 选中
chkbox.Checked = true;
}
}
}
Label5.Text = "全不选"; }
// 取消全选
if (checkedAll.Checked.Equals(false))
{
int i = 0;
for (i = 0; i < dl1.Items.Count; i++)
{
DataList dl2 = dl1.Items[i].FindControl("dl2") as DataList;
if (dl2 != null)
{
for (int j = 0; j < dl2.Items.Count; j++)
{
HtmlInputCheckBox chkbox = dl2.Items[j].FindControl("chkSelect") as HtmlInputCheckBox;
// 选中
chkbox.Checked = false;
}
}
}
Label5.Text = "全选"; }
}
增加 全选按钮的 checkbox的click事件
根据 Class属性,获取整个checkbox组,遍历选中
<script src="Scripts/jquery-1.7.1.js"></script>
<form>
<input type="checkbox" id="cboMain" class="cbo"/> <input type="checkbox" class="cbo"/>
<input type="checkbox" class="cbo"/>
<input type="checkbox" class="cbo"/>
<input type="checkbox" class="cbo"/>
</form>
</body>
<script>
$('#cboMain').click(function () {
var result = $(this).is(':checked');
alert(result);
$('.cbo').attr("checked", result);
})
</script>