C# 从数据库中选择Radcheckboxlist

C# 从数据库中选择Radcheckboxlist,c#,telerik,checkboxlist,C#,Telerik,Checkboxlist,页面加载时,将显示列表项。但当我按下搜索按钮时,它们消失了 此处代码(ascx): 我从db得到了正确/错误的答案。我希望我能读到它们,并把它们填在列表中。但回发完成后,cblist显示为空 除了对象。ToBoolean() 如果对我来说,这里有一个快速解决方案: foreach (ButtonListItem Item in cblDurum.Items) { Item.Selected = (bool)dt.Rows[0][Item.Text]; // OR With

页面加载时,将显示列表项。但当我按下搜索按钮时,它们消失了

此处代码(ascx):

我从db得到了正确/错误的答案。我希望我能读到它们,并把它们填在列表中。但回发完成后,cblist显示为空


除了
对象。ToBoolean()

如果对我来说,这里有一个快速解决方案:

foreach (ButtonListItem Item in cblDurum.Items)
{

    Item.Selected = (bool)dt.Rows[0][Item.Text];

    // OR With
    Item.Selected = ((DataRow)dt.Rows[0]).Field<bool>(Item.Text);

    // OR BY Item.Value
   int IntValue;
   if ( int.TryParse(Item.Value, out IntValue) )
       Item.Selected = (bool)dt.Rows[0].ItemArray[IntValue]
}
foreach(cblDurum.Items中的按钮列表项)
{
Item.Selected=(bool)dt.Rows[0][Item.Text];
//或与
Item.Selected=((DataRow)dt.Rows[0]).Field(Item.Text);
//或按项目。值
int值;
if(int.TryParse(Item.Value,out IntValue))
Item.Selected=(bool)dt.Rows[0]。ItemArray[IntValue]
}
如果出现以下情况,则会引发错误:

  • 当没有行时,dt.行[0]
  • 字段(Item.Text),如果没有名为类似Item.Text的字段
  • 如果不可能,则从对象强制转换为布尔值

  • 到目前为止,最好的方法是使用。

    这段代码在
    页面加载()
    中吗?您有任何错误吗?Object.ToBoolean()此代码位于按钮中。没有错误。复选框列表项消失了。你的问题在别处,在一个简单的项目中一切都很好。尽量孤立你的问题。当你发现如何重现这个错误时,你就会知道如何修复它。检查你的代码中是否有
    clbDurum.Visible
    。仅允许在
    clbDurum
    声明(runnat id)上显示重要属性。尚未选择任何项目。数据库发送给我true/false,如果存在true,则cblDurum.Items[0]。必须选择Selected。但是整个项目和文本都消失了。你在问题中显示的代码没有问题,它可以工作。我理解你的问题。但问题中的代码并不是问题的根源。我已经对这个问题进行了评论,为您提供了一条快速的指南,以找到行为的来源。
    var result = apuSrv.GetInfos(txtWO.Text);
    DataTable dt = result.ToDataTable();
    if (dt.Rows[0]["PS"].ToBoolean())
        cblDurum.Items[0].Selected = true;
    if (dt.Rows[0]["LC"].ToBoolean())
        cblDurum.Items[1].Selected = true;
    if (dt.Rows[0]["GB"].ToBoolean())
        cblDurum.Items[2].Selected = true;
    if (dt.Rows[0]["MINOR"].ToBoolean())
        cblDurum.Items[3].Selected = true;
    if (dt.Rows[0]["TEST"].ToBoolean())
        cblDurum.Items[4].Selected = true;
    if (dt.Rows[0]["OTHER"].ToBoolean())
        cblDurum.Items[5].Selected = true;
    if (dt.Rows[0]["PRESERVATION"].ToBoolean())
        cblDurum.Items[6].Selected = true;
    if (dt.Rows[0]["BORESCOPE"].ToBoolean())
        cblDurum.Items[7].Selected = true;
    if (dt.Rows[0]["INN"].ToBoolean())
        cblDurum.Items[8].Selected = true;
    if (dt.Rows[0]["OUT"].ToBoolean())
        cblDurum.Items[9].Selected = true;
    if (dt.Rows[0]["RECEIVING_TEST"].ToBoolean())
        cblDurum.Items[10].Selected = true;
    
    foreach (ButtonListItem Item in cblDurum.Items)
    {
    
        Item.Selected = (bool)dt.Rows[0][Item.Text];
    
        // OR With
        Item.Selected = ((DataRow)dt.Rows[0]).Field<bool>(Item.Text);
    
        // OR BY Item.Value
       int IntValue;
       if ( int.TryParse(Item.Value, out IntValue) )
           Item.Selected = (bool)dt.Rows[0].ItemArray[IntValue]
    }