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.
WiggingAsked:
Who is Participating?

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

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

robastaCommented:
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)
        }


0
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
0
robastaCommented:
oops, change that from [0] to (0)
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 below.......how do i find image2 from pageload?  



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

<ItemStyle Width="30px"></ItemStyle>
                                                </asp:TemplateField>
                          
                                
                                                  <asp:TemplateField ItemStyle-HorizontalAlign="Left">
                                                  <ItemTemplate>
                                                  <table border="0" cellspacing="0" cellpadding="3" >
<tr>
<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" />
                      </a></td>
</tr>
<tr>
<td colspan="2">
<asp:label ID="item"    Text='<%# Eval("short_desc") %>' runat="server" />
</td>
<tr>
</table>

                             </ItemTemplate>             
                    </asp:TemplateField>                             
                                          
                           
                                          
                                        </Columns>       
                                      
                                          <SelectedRowStyle ForeColor="#F5853B" />
                                      
      </asp:GridView>

Open in new window

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

http://forums.asp.net/t/998368.aspx
0

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

        Else

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

Open in new window

0
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
ASP.NET

From novice to tech pro — start learning today.