• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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.
  • 3
  • 3
1 Solution
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)
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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"  >      
                                      <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:

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now