Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListView中的FindControl(ImageButton)_C#_Asp.net - Fatal编程技术网

C# ListView中的FindControl(ImageButton)

C# ListView中的FindControl(ImageButton),c#,asp.net,C#,Asp.net,我希望有人能帮我。我无法在ListView中找到ImageButton控件,它总是给我对象引用未设置为对象的实例错误 场景是,如果我选中复选框,我希望imagebutton更改其“图像”。该复选框不在Listview ItemTemplate中。以下是我的checkbox\u checkchanged事件的隐藏代码: if (cb.Checked) { foreach (Control c in lv.Controls) {

我希望有人能帮我。我无法在ListView中找到ImageButton控件,它总是给我对象引用未设置为对象的实例错误

场景是,如果我选中复选框,我希望imagebutton更改其“图像”。该复选框不在Listview ItemTemplate中。以下是我的checkbox\u checkchanged事件的隐藏代码:

   if (cb.Checked)
    {
        foreach (Control c in lv.Controls)
        {
            ImageButton btndel = (ImageButton)c.FindControl("btnDelete");
            btndel.ImageUrl = "~/images/activate.png";
        }
     }

注意:我使用ForEach循环时认为btnDelete按钮在我的Listview中出现了几次。

在强制转换之前,您应该检查FindControl的结果是否为null或类型是否正确


尝试在
c
对象的控件内调用FindControl,因为它可能是您要查找的控件的下一层嵌套(因此在ListViewItem内),实际上,您可以在
lv.Items
内循环,并在其中执行FindControl…

如果复选框在ListView之外,最好的方法是使用ListView的:

您不需要处理复选框“CheckedChanged”事件,只需要在aspx标记上添加OnItemCreated处理程序:

<asp:ListView ID="LV" OnItemCreated="LV_ItemCreated" ... />


这样可以防止ListView项的多次迭代。无论如何,每次回发都会隐式调用ItemCreated以重新创建ListView项目。

。我们使用标签。非常感谢您的回复!Tim,您提供的代码与我在ASP.net论坛中找到的代码完全相同,由Dwhite回复[1]:它正在工作。非常感谢各位!我真的非常感谢it@bounce:欢迎来到Stackoverflow。很高兴我能帮上忙。看一看。Tim,我正在努力理解你的代码,具体条件是:如果(item.ItemType==ListViewItemType.DataItem),你能解释一下我们在这里比较的是什么以及这是为了什么吗?@Bounce:看看什么是
ListViewItem.ItemType
For each (ListViewDataItem c in lv.items)...
For each (ListViewDataItem c in lv.items)...