ListView FindControl error
I have the next error:
System.NullReferenceException – Object reference not set to an instance of an object.
To the next code:
<asp:ListView ID="LV1" runat="server" DataSourceID="LinqDataSource">
<ItemTemplate>
<asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUr开发者_JAVA百科l='<%# Eval("ImageUrl") %>' />
//....and so on till the
</asp:ListView>
The code - behind:
protected void checkTheImage()
{
((Image)LV1.FindControl("Image1")).ImageUrl = "(noImage.jpg)" ;
}
and the code on page_load:
protected void Page_Load(object sender, EventArgs e)
{
checkTheImage();
}
Why i got the error? what is wrong in my code?
You have to specify the item:
protected void checkTheImage()
{
((Image)LV1.Items[0].FindControl("Image1")).ImageUrl = "(noImage.jpg)" ;
}
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)" ;
}
精彩评论