ItemCreated FindControl problem c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

All I need to do is set the border property of an image control to border=0.
I want to do this at runtime for the first image on the page in a datalist.
In the ItemCreated event I'm trying to use FindControl to set a reference to
the img control so I can change the attribute. But when I run the code below
I get the error: "Object reference not set to an instance of an object." I
assume its not finding the control and returning null? Anyway, you can see
what I'm trying to do, can anyone tell me how to get it to work? (code below)

******* code *******
public void main_ItemCreated(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
HtmlImage imgPatron = new HtmlImage();
imgPatron = (HtmlImage)e.Item.FindControl(Request.Form["patron"]);
if(imgPatron!=null)
{
image_count();
if(i_count == 1)
{
imgPatron.Border = 0;
}
}

}

************

Thanks for any help!
 
Wrong event. I recommend using PreRender event since the page is fully built
at that stage. You can use ItemDataBound if you wish, but PreRender is
easier.

Eliyahu
 
What is exactly the "Request.Form["patron"]"?

I think you should write:


.....FindControl(ID/name of the server side control);


Hope this help
 
Back
Top