• 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.
0
Wigging
Asked:
Wigging
  • 3
  • 3
1 Solution
 
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
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"  >      
                                          <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
 
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
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