access image contril inside datalist from pageload

is this correct?
  Dim image2 As Image = CType(DataList2.FindControl("image2"), Image)

i get error below
Object reference not set to an instance of an object.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

not correct.

on a datalist, controls are inside the DataListItem control. so, to get the image for the first record, you would say:

Dim img2 as Image = CType(DataList2.Controls[0].FindControl("image2"),Image)

for the second item
Dim img2 as Image =  CType(DataList2.Controls[1].FindControl("image2"),Image)  etc.


You can also do this:
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
          Dim img2 as Image =  CType( e.Item.FindControl("image2"),Image)

WiggingAuthor Commented:
when I add this on page load   Dim img2 as Image = CType(DataList2.Controls[0].FindControl("image2"),Image)

i get i"dentifier expected" error highlighting the number inside the brackets
oops, change that from [0] to (0)
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

WiggingAuthor Commented:
look at my grid do i find image2 from pageload?  

<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false" 
                                      ShowHeader="False"   CellPadding="4" 
          CellSpacing="0"  GridLines="None"  >      
                                      <asp:TemplateField ItemStyle-Width="30px" >
                                               <asp:HyperLink ID="HyperLink1" runat="server"> <asp:Image ID="Image1" runat="server" ImageUrl="images/radioNoChk.jpg" /></asp:HyperLink>  

<ItemStyle Width="30px"></ItemStyle>
                                                  <asp:TemplateField ItemStyle-HorizontalAlign="Left">
                                                  <table border="0" cellspacing="0" cellpadding="3" >
<td><asp:label ID="engine"   Text='<%# Eval("option_desc") %>' runat="server" CssClass="titlebolditem" /></td>
<td align="right"><asp:Label ID="Label2" runat="server" CssClass="titlebolditem" 
                        Text='<%# Eval("promo_amt","{0:+ $#,##0.00;- $#,##0.00;$0.00}") %>' /></td>
<td rowspan="2"><a href='details.aspx?id=<%# Eval("option_num")%> &amp;option=<%# request.querystring("i")%>' 
                          onclick='return popitup(&#039;details.aspx?id=<%# Eval("option_num")%>&amp;option=<%# request.querystring("i")%>&#039;)'>
                      <asp:Image ID="image2" runat="server" />
<td colspan="2">
<asp:label ID="item"    Text='<%# Eval("short_desc") %>' runat="server" />

                                          <SelectedRowStyle ForeColor="#F5853B" />

Open in new window

Do you want PageLoad? Remember the ITemTemplate represents a single row from the bound data. Take a look at this thread:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WiggingAuthor Commented:
yes because what i am doing is I have a chechbox control in my master page...  and when people check this checkbox it postbacks and in child page where my grid is i would like to hide image or show based on checkbox value.  

any ideas ???  look at code below that is inside my pageload event
        Dim email As String = Me.Master.EMail
        ' Dim image2 As Image = CType(GridView1.Controls(1).FindControl("image2"), Image)
        '  Dim image3 = CType(GridView1.Rows(0).Controls(1), Image)

        If email = True Then
            'image2.Visible = True
            '  image3.Visible = True


            'image2.Visible = False
            'image3.Visible = False
        End If

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.