在项目中我们可能会经常遇到一收集多选信息的情况,比如做注册的时候要收集个人爱好,那时候大家第一个想到的肯定是CheckBoxList。那我们怎么来获取到CheckBoxList的值并且存入数据库呢??

如果我们还需要编辑用户的个人信息而其中爱好也是可以改动的,此时同样大家也会想用CheckBoxList去显示用户的各人信息,那我们又要如何将库里的值用CheckBoxList表示出来呢?? 编辑分析问题
遇到这种情况大家肯定想到for,foreach去遍历,没错这样完没有问题,不管是获取CheckBoxList的值还是设置CheckBoxList的值我们都可以用遍历去实现。而我这里将大家常用的方法总结了一下,做了两个方法。这样用起来能更方便灵活。

举个例子:我们要收集某某公司员工的信息其中一项是爱好。而且要求员工信息可以改动

我们选用了CheckBoxList来实现爱好的收集和显示

方法:

......

1.收集时,将CheckBoxList里选中的项转换成字符串,并用“,”隔开
这里只要调用方法GetChecked(CheckBoxList checkList, string separator)
就可以获取到想要的数据。然后存入数据库。

2.显示时,先从库里获取爱好的数据(刚刚用“,”隔开的字符串),
 然后调用方法SetChecked(CheckBoxList checkList,string selval,string separator)
就可以将库里的数据用CheckBoxList的形式表现出来

......
方法的使用: 
//这里获取CheckBoxList中的选中项并用","隔开
string str=GetChecked(this.checkList1, ",");
......

//这里是将str这个字符串的值又设回CheckBoxList
SetChecked(this.checkList1,str,","); /// <summary>
/// 初始化CheckBoxList中哪些是选中了的 /// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param>
/// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>
public static string SetChecked(CheckBoxList checkList,string selval,string separator)
{
selval = separator + selval + separator; //例如:"0,1,1,2,1"->",0,1,1,2,1,"
for(int i=; i<checkList.Items.Count; i++)
{
checkList.Items[i].Selected = false;
string val = separator + checkList.Items[i].Value + separator;
if(selval.IndexOf(val)!=-)
{
checkList.Items[i].Selected = true;
selval = selval.Replace(val,separator); //然后从原来的值串中删除已经选中了的
if(selval == separator) //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符
{
selval += separator; //添加一个分隔符
}
}
}
selval = selval.Substring(,selval.Length-); //除去前后加的分割符号
return selval;
} /// <summary>
/// 得到CheckBoxList中选中了的值
/// </summary>
/// <param name="checkList">CheckBoxList</param>
/// <param name="separator">分割符号</param>
/// <returns></returns>
public static string GetChecked(CheckBoxList checkList, string separator)
{
string selval = "";
for(int i=;i<checkList.Items.Count;i++)
{
if(checkList.Items[i].Selected)
{
selval += checkList.Items[i].Value + separator;
}
}
return selval;
}

全选的是  添加个button  这个单击事件写:

  for (int b = ; b < CheckBoxList1.Items.Count; b++)
{
this.CheckBoxList1.Items[b].Selected = true;
}

引用自  http://www.cnblogs.com/shawker/archive/2009/03/17/1414795.html

做出来的题

空间拖成这样

前台代码

 <form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Number" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="选择">
<HeaderStyle HorizontalAlign="Center" Height="25px" Width="45px" />
<ItemTemplate>
<asp:CheckBox ID="ckb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:BoundField DataField="Number" HeaderText="Number" InsertVisible="False" ReadOnly="True" SortExpression="Number" />
<asp:BoundField DataField="NumName" HeaderText="NumName" SortExpression="NumName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DemoConnectionString %>" SelectCommand="SELECT * FROM [DataTb]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
</form>

后台代码

 protected void Button1_Click(object sender, EventArgs e)
{
string PKname = "";
foreach (GridViewRow GR in this.GridView1.Rows)
{
CheckBox CB = (CheckBox)GR.FindControl("ckb");
if (CB.Checked)
{
PKname += this.GridView1.DataKeys[GR.RowIndex].Value.ToString() + ",";
}
}
Response.Write(PKname);
} protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow GR in this.GridView1.Rows)
{
CheckBox CB = (CheckBox)GR.FindControl("ckb");
CB.Checked = true;
}
}

后台获取全选的方法.

     protected void Page_Load(object sender, EventArgs e)
{
CheckBox cbAll = (CheckBox)GridView1.HeaderRow.FindControl("sss");
if (cbAll.Checked)
{
NewMethod();
}
else
{
NoNewMethod(); }
}

那两个方法是

  private void NewMethod()
{
foreach (GridViewRow GR in this.GridView1.Rows)
{
CheckBox CB = (CheckBox)GR.FindControl("ckb");
CB.Checked = true;
}
}
private void NoNewMethod()
{
foreach (GridViewRow GR in this.GridView1.Rows)
{
CheckBox CB = (CheckBox)GR.FindControl("ckb");
CB.Checked = false;
}
}

前台js获取全选 和选中变色的方法

<script>
function SelectAll(tempControl) {
//将除头模板中的其它所有的CheckBox取反
var theBox = tempControl;
xState = theBox.checked;
elem = theBox.form.elements;
for (i = 0; i < elem.length; i++)
if (elem[i].type == 'checkbox' && elem[i].id != theBox.id) {
if (elem[i].checked != xState)
elem[i].click();
} }
function changecolor(cbo, o) {
var theBox = cbo;
var tr = document.getElementById(o);
if (theBox.checked) {
tr.style.backgroundColor = "Red";
}
else {
tr.style.backgroundColor = "#CCCCCC"; }
}
function ttt() {
alert("aaa");
}
</script>

使用js的方法

 <HeaderTemplate>
<asp:CheckBox ID="sss" runat="server" Text="全选" onclick="javascript:SelectAll(this);" />
</HeaderTemplate>

repeater编辑check是否被选中的方法

  string ids = "";
for (int i = ; i < Repeater1.Items.Count; i++)
{
//找到相应的Checkbox控件
CheckBox cb = (CheckBox)Repeater1.Items[i].FindControl("chk_word1");
//获取id并连接成字符串,用,分隔
if (cb != null)
{
string selectedId = cb.ToolTip;
if (cb.Checked)
{
ids += selectedId;
}
}
}

前台中的chk_word1是

<asp:CheckBox ID="chk_word1" ToolTip='<%#Eval("id")%>' runat="server" />

另外上面的用foreach循环的方法 更简单

    foreach (RepeaterItem rt in this.Repeater1.Items)
{
CheckBox cb = (CheckBox)rt.FindControl("chk_word1");
//获取id并连接成字符串,用,分隔
if (cb != null)
{
string selectedId = cb.ToolTip;
//判断是否被选中
if (cb.Checked)
{
//可以获取循环别的空间
              HiddenField hid1 = (HiddenField)rt.FindControl("CPhone1");
HiddenField hid2 = (HiddenField)rt.FindControl("CPhone2");
ids += hid1.Value;
ids += hid2.Value;
ids += selectedId;
}
}
}

CheckBoxList的操作查询是否被选中设置或者得到的更多相关文章

  1. mysql查询缓存打开、设置、参数查询、性能变量意思

    http://blog.sina.com.cn/s/blog_75ad10100101by7j.html http://www.cnblogs.com/zemliu/archive/2013/08/0 ...

  2. sqlserver -- 学习笔记(三)解决php连接sqlserver2005视图时显示“异类查询要求为连接设置 ANSI_NULLS 和 ANSI_WARNINGS 选项”的问题

    --php5.2 --sqlserver2005 php连接sqlserver的视图aa,语句如下: $query = mssql_query("select * from dbo.aa&q ...

  3. 一对一关联查询时使用relation连贯操作查询后,调用getLastSql()方法输出的sql语句

    如题: 一对一关联查询时使用relation连贯操作查询后,调用getLastSql()方法输出的sql语句不是一条关联查询语句. 例如: $list = $db->relation(true) ...

  4. MySQL查询in操作 查询结果按in集合顺序显示_Mysql_脚本之家

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  5. 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体

    [源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...

  6. jQuery操作CheckBox的方法(选中,取消,取值)

    jQuery操作CheckBox的方法(选中,取消,取值). 代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/ ...

  7. Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理

    Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理 1. reg 工具 这个cli工具接口有,优先使用,jreg的要调用dll了,麻烦的 ...

  8. Hibernate 查询,返回结果设置到DTO

    1:原生sql的查询,返回结果设置到DTO: Query query = sessionFactoryRtData.getCurrentSession().createSQLQuery(hql.toS ...

  9. 详解慢查询日志的相关设置及mysqldumpslow工具

    概述 mysql慢查询日志是mysql提供的一种日志记录,它是用来记录在mysql中相应时间超过阈值的语句,就是指运行时间超过long_query_time值的sql,会被记录在慢查询日志中.long ...

随机推荐

  1. find . / -newer oldest_file.txt ! -newer newest_file.txt

    如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项. 它的一般形式为: $ find . / -newer oldest_file.txt ! -newer newe ...

  2. C 文件直接包含

    C 文件直接包含 有一部分代码很大,在很多函数中重复,可以直接写在另外的一个文件中,引用时直接包含.co.cpp两个函数都 包含c1.cxx. 点击(此处)折叠或打开 ////// co.cpp #i ...

  3. 简单实用的日志类CLog (Python版)

    #coding: utf-8 import time ''' /***************************************************************** Fu ...

  4. HTML事件

    Media 事件

  5. 注册Dev的帮助文件

    Download the CHM files from… Code: https://www.devexpress.com/Support/Documentation/download.xml?pla ...

  6. 深入分析 Java 中的中文编码问题(1)

    几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...

  7. delphi 使用superobject实现jsonrpc的http远程调用 good

    http://blog.csdn.net/earbao/article/details/46423167

  8. Leetcode-Database-176-Second Highest Salary-Easy(转)

    leetcode地址:https://oj.leetcode.com/problems/second-highest-salary/ 这个问题很有趣,是要求我们写个sql来查询Employee表里第二 ...

  9. Mysql rr和rc隔离

    REPEATABLE READ This is the default isolation level for InnoDB. For consistent reads, there is an im ...

  10. Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数

    4.2.7. 去除控制字符:removeCtlChr Python语言总结4.2. 和字符串(str,unicode等)处理有关的函数Sidebar     Prev | Up | Next4.2.7 ...