当前位置:编程学习 > C#/ASP.NET >>

CheckBoxList从数据库中取出数据.怎样实现?我是一名初学者,请求帮助?

<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal"
                        RepeatLayout="Flow"  AutoPostBack="True">
                        <asp:ListItem Value="101">德国</asp:ListItem>
                        <asp:ListItem Value="102">荷兰</asp:ListItem>
                        <asp:ListItem Value="103">巴西</asp:ListItem>
                    </asp:CheckBoxList>


我想实现从SQL中取出数据,在CheckBoxList上,自动打上勾,请求帮助,谢谢? --------------------编程问答-------------------- for( int a = 0 ; a < CheckBoxList1.Items.Count ; a ++ )
{
if( CheckBoxList1.Items[a].Value == 数据库取出来的值 )
{
CheckBoxList1.Items[a].Selected =  true ;
break;
}
} --------------------编程问答-------------------- 1。
问题没表述清楚

2。
CheckBoxList1.DataTextField = "字段名称1";
CheckBoxList1.DataValueField = "字段名称2";
CheckBoxList1.DataSource = GetMyDataSource(); // 从数据库获取数据集合
CheckBoxList1.DataBind();

3。
foreach(ListItem item in CheckBoxList1.Items)
{
 if(item.Value == 数据库取出来的值 ) {
    item.selected = true;
    break;
 }
} --------------------编程问答-------------------- for( int a = 0 ; a < CheckBoxList1.Items.Count ; a ++ )
{
if( CheckBoxList1.Items[a].Value == 数据库取出来的值 )
{
CheckBoxList1.Items[a].Selected =  true ;
break;
}
}
--------------------编程问答-------------------- 我刚才运行拉,还有问题,您能不能把代码写的细致一点,把代码要放在什么地方,我学,net才一个月不到,麻烦拉,我会加油的,谢谢拉 --------------------编程问答-------------------- 当CheckBoxList绑定结束后,提取数据值到DATASET,

遍历DATASET中的值与每一个比较。

看看能不能找段代码 --------------------编程问答-------------------- 昨夜西风凋碧树 --------------------编程问答-------------------- sbqcel(空空儿) ( ) 信誉:98    Blog   加为好友  2007-06-14 15:52:56  得分: 0  
 
 
   for( int a = 0 ; a < CheckBoxList1.Items.Count ; a ++ )
{
if( CheckBoxList1.Items[a].Value == 数据库取出来的值 )
{
CheckBoxList1.Items[a].Selected =  true ;
break;
}
}
  
 

代码写在Page_Load里就行,最后写成函数,然后在Page_Load里面调用 --------------------编程问答-------------------- 最主要的就是上面的了,不知道你怎么存的数据,怎么写细致?
假设我有一个字段存储的是CheckBoxList1的值,以逗号分割:101,103

string strValue = "101,103";//这个值是从数据库取出来的
initialCheckBoxList(strValue);

private void initialCheckBoxList(string strValue)
{
string[] array = strValue.Split( ',' );
for( int i = 0 ; i < array.Length - 1 ; i ++ )
{
for( int a = 0 ; a < CheckBoxList1.Items.Count ; a ++ )
{
if( CheckBoxList1.Items[a].Value == array[i] )
{
CheckBoxList1.Items[a].Selected =  true ;
break;
}
}
}
} --------------------编程问答-------------------- if( CheckBoxList1.Items[a].Value == 数据库取出来的值 )
数据库取出来的值-数据应该会取吧 --------------------编程问答-------------------- sbqcel(空空儿) 
说的对 --------------------编程问答-------------------- 如果你取出来的是个表集,那么就循环表集的数据传进去处理了
那么方法initialCheckBoxList就要改了
主要思路就是将数据库里的数据和CheckBoxList1项的值作比较,是就项的Selected =  true --------------------编程问答-------------------- Chk.Checked = DataReader.GetBoolean(列表值); --------------------编程问答-------------------- 顶一下,怎么我的也不对呢?? --------------------编程问答-------------------- 我得也是这问题 --------------------编程问答-------------------- 参考这篇:
http://topic.csdn.net/u/20110617/17/0305d54b-b540-4726-8014-f063fd067a29.html --------------------编程问答-------------------- --------------------编程问答-------------------- 像你这个多选的情况存的时候 用“,”遍历存入 数据库的一个字段。
然后读取:
 string list = 这个是你从数据库读的记录多选的字段(test,test1,test2)这个格式;
        string[] listyd = list .Split(',');//然后用“,”分隔
         //遍历
        foreach (ListItem liy in this.CheckboxList1.Items)
        {
            if (listyd.Contains(liy.Value))
            {
                liy.Selected = true;
            }
        }
--------------------编程问答--------------------
引用 16 楼 aig003 的回复:
 这种方法比较简单
--------------------编程问答--------------------
引用 2 楼 jinglecat 的回复:
1。
问题没表述清楚

2。
CheckBoxList1.DataTextField   =   "字段名称1 ";
CheckBoxList1.DataValueField   =   "字段名称2 ";
CheckBoxList1.DataSource   =   GetMyDataSource();   //   从数据库获取数据集合
CheckBoxList1.DataBin……


这个就行的。  --------------------编程问答--------------------
引用 17 楼 seanding1989 的回复:
像你这个多选的情况存的时候 用“,”遍历存入 数据库的一个字段。
然后读取:

C# code
 string list = 这个是你从数据库读的记录多选的字段(test,test1,test2)这个格式;
        string[] listyd = list .Split(',');//然后用“,”分隔
         //遍历
        foreach (List……
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,