本文介绍了ListView控件的FindControl错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个错误:
System.NullReferenceException - 对象引用不设置到对象的实例
要在未来code:
<asp:ListView ID="LV1" runat="server" DataSourceID="LinqDataSource">
<ItemTemplate>
<asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
//....and so on till the
</asp:ListView>
在code - 背后:
The code - behind:
protected void checkTheImage()
{
((Image)LV1.FindControl("Image1")).ImageUrl = "(noImage.jpg)" ;
}
和在Page_Load中的code:
and the code on page_load:
protected void Page_Load(object sender, EventArgs e)
{
checkTheImage();
}
为什么我得到了错误?什么是错在我的code?
Why i got the error? what is wrong in my code?
推荐答案
您必须指定项目:
protected void checkTheImage()
{
((Image)LV1.Items[0].FindControl("Image1")).ImageUrl = "(noImage.jpg)" ;
}
由于ListView控件呈现给每个孩子一个项目Image1的控制。要改变所有图像:
because the ListView render an Image1 control for each child item. To change all images:
protected void checkTheImage()
{
foreach(ListViewItem item in LV1.Items)
((Image)item.FindControl("Image1")).ImageUrl = "(noImage.jpg)" ;
}
这篇关于ListView控件的FindControl错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!