ItemCreated FindControl problem c#

Discussion in 'ASP .Net' started by =?Utf-8?B?dHBhcmtzNjk=?=, Jun 16, 2005.

  1. 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!
     
    =?Utf-8?B?dHBhcmtzNjk=?=, Jun 16, 2005
    #1
    1. Advertising

  2. 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

    "tparks69" <> wrote in message
    news:...
    > 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!
     
    Eliyahu Goldin, Jun 16, 2005
    #2
    1. Advertising

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

    I think you should write:


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


    Hope this help

    "tparks69" wrote:

    > 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!
     
    =?Utf-8?B?QkVMSVVT?=, Jun 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lucy
    Replies:
    0
    Views:
    312
  2. Learning SQL Server

    Re: getting column value in ItemCreated datagrid event

    Learning SQL Server, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    552
    Learning SQL Server
    Aug 12, 2003
  3. Hardy Wang

    DataList's ItemCreated event

    Hardy Wang, Oct 27, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,585
    Hardy Wang
    Oct 27, 2003
  4. Lars Netzel

    DataGrid Itemcreated Problem!

    Lars Netzel, Dec 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    488
    Matt Berther
    Dec 9, 2004
  5. =?Utf-8?B?dHBhcmtzNjk=?=

    ItemCreated event problem

    =?Utf-8?B?dHBhcmtzNjk=?=, Jun 16, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    =?Utf-8?B?dHBhcmtzNjk=?=
    Jun 16, 2005
Loading...

Share This Page