Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a nested Image from code behind

Status
Not open for further replies.

belle9

Programmer
Nov 3, 2004
87
US
I'm trying to set the visibility of imgArrow when an item in my datalist dlProd is clicked on.
Here is my image:
Code:
<asp:datalist id="dlCategories" runat="server">
										<ItemTemplate>
											<P class="wht_14" style="MARGIN: 8px 18px 2px 0px" align="right"><b>
													
													<asp:LinkButton id="Linkbutton1" Runat="server" CommandArgument='<%# Container.DataItem("categoryID") %>' CommandName="Edit">
														<%#Container.DataItem("CategoryName")%>
													</asp:LinkButton></b>
													</P>
											<!-- Begin Product Listing -->
											
											<asp:Panel id="PanelProd" runat="server" Visible="false">
												<asp:DataList ID="dlProd" Runat="server">
													<ItemTemplate>
														<P class="wht_11" style="MARGIN: 0px 26px 4px 0px" align="right">
														<asp:Image ID="imgArrow" Runat="server" Visible="False" ImageUrl="/images/ico_arrowRight.gif"></asp:Image>
														<asp:LinkButton id="linkButton2" Runat="server" OnClick="ProductDetailClicked" CommandArgument='<%# Container.DataItem("productsID") %>'>
																<%#Container.DataItem("ProductName")%>
															</asp:LinkButton></P>
													</ItemTemplate>
												</asp:DataList>
												
											</asp:Panel>
											<!-- End Product Listing -->
										</ItemTemplate>
									</asp:datalist>

Here's the code behind:

Code:
Public Sub ProductDetailClicked(ByVal sender As Object, ByVal e As EventArgs)

        PanelMainProducts.Visible = False
        PanelProduct.Visible = True
        myConnection = New SqlConnection(conString)
        myConnection.Open()

        mycommand = New SqlCommand("getSpecificProduct", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

        Dim objButton As LinkButton

        If Session("ProdID") = "" Then
            objButton = CType(sender, LinkButton)
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = objButton.CommandArgument
        Else
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = sender
        End If

        myReader = mycommand.ExecuteReader

        Me.Bind_Data(myReader)

        myConnection.Close()
  
        imgArrow = CType(Me.dlCategories.FindControl("imgArrow"), System.Web.UI.WebControls.Image)
        imgArrow.Visible = True




    End Sub

I keep getting the error: Object ref not set to instance of an object...

Any ideas?
 
Dim imgArrow as Image

imgArrow = CType(Me.dlCategories.FindControl("imgArrow"), System.Web.UI.WebControls.Image)
imgArrow.Visible = True

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
That's exactly what I have, but I get:
System.NullReferenceException: Object reference not set to an instance of an object.

The only difference between mine and yours is that I dim imgArrow as System.web.UI.WebControls.Image. Using just Image gives an ambiguous type error.
 
i didn't see the Dim statement on yours...is this button outside of your datagrid?

because then you look to be just calling the datagrid in general here:

imgArrow = CType(Me.dlCategories.FindControl("imgArrow"), System.Web.UI.WebControls.Image)

where you may need something like this
Code:
imgArrow = CType(Me.dlCategories.Items(ITEMINDEX).FindControl("imgArrow"), System.Web.UI.WebControls.Image)[/

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
The image is nested like this:

Datalist(dlCategories)
Panel(panelProd)
Datalist(dlPRoducts)
IMAGE HERE(imgArrow)

I had defined the image all the way at the top, like so:
Protected WithEvents imgArrow As System.Web.UI.WebControls.Image
 
but you need to do this on the button click...

dim img as System.Web.UI.WebControls.Image
dim i as Integer = 'The index that you want to see the image for...

img = CType(Me.dlCategories.Items(i).FindControl("imgArrow"), System.Web.UI.WebControls.Image)

img.visible = True

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Doing this doesn't either work:

Code:
  Dim imgArrow As System.web.UI.WebControls.Image
        Dim i As Integer

        Do While i < Me.dlCategories.Items.Count

            imgArrow = CType(Me.dlCategories.Items(i).FindControl("imgArrow"), System.Web.UI.WebControls.Image)
            imgArrow.Visible = True
        Loop

What's wrong?? It seems so simple..
 
I am doing it on the button click:

Code:
 Public Sub ProductDetailClicked(ByVal sender As Object, ByVal e As EventArgs)

        PanelMainProducts.Visible = False
        PanelProduct.Visible = True
        myConnection = New SqlConnection(conString)
        myConnection.Open()

        mycommand = New SqlCommand("getSpecificProduct", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

        Dim objButton As LinkButton

        If Session("ProdID") = "" Then
            objButton = CType(sender, LinkButton)
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = objButton.CommandArgument
        Else
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = sender
        End If

        myReader = mycommand.ExecuteReader

        Me.Bind_Data(myReader)

        myConnection.Close()

        Dim imgArrow As System.web.UI.WebControls.Image
        Dim i As Integer

        Do While i < Me.dlCategories.Items.Count

            imgArrow = CType(Me.dlCategories.Items(i).FindControl("imgArrow"), System.Web.UI.WebControls.Image)
            imgArrow.Visible = True
        Loop

    End Sub

Code:
<asp:datalist id="dlCategories" runat="server">
     <ItemTemplate>
         <P class="wht_14" style="MARGIN: 8px 18px 2px 0px" align="right"><b>
         <asp:LinkButton id="Linkbutton1" Runat="server" CommandArgument='<%# Container.DataItem("categoryID") %>' CommandName="Edit"><%#Container.DataItem("CategoryName")%></asp:LinkButton></b></P>
         <!-- Begin Product Listing -->
         <asp:Panel id="PanelProd" runat="server" Visible="false">
              <asp:DataList ID="dlProd" Runat="server">
                   <ItemTemplate>
	               <P class="wht_11" style="MARGIN: 0px 26px 4px 0px" align="right">
                       <asp:Image ID="imgArrow" Runat="server" Visible="False" ImageUrl="/images/ico_arrowRight.gif"></asp:Image>
                      <asp:LinkButton id="linkButton2" Runat="server" OnClick="ProductDetailClicked" CommandArgument='<%# Container.DataItem("productsID") %>'><%#Container.DataItem("ProductName")%></asp:LinkButton></P>
                   </ItemTemplate>
              </asp:DataList>
         </asp:Panel>
         <!-- End Product Listing -->
     </ItemTemplate>
</asp:datalist>
 
your loop doesn't increment i or give it an initial value...that won't work then...

Code:
Dim imgArrow As System.web.UI.WebControls.Image
Dim i As Integer = 0
Do While i < Me.dlCategories.Items.Count
  imgArrow = CType(Me.dlCategories.Items(i).FindControl("imgArrow"), System.Web.UI.WebControls.Image)
  imgArrow.Visible = True
  i = i + 1
Loop

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Go point re incrementing i, however that didn't do it...
 
is this button within the datalist? if so, just do a simple response.Write to see if you're even getting to the button...if you're in the datalist, i have a feeling that you're not getting to this click event at all.

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
I know I'm getting to the button because I'm debugging the code and stepping through each line...The line imgArrow = CType(Me.dlCategories.Items(i).FindControl("imgArrow"), System.Web.UI.WebControls.Image) executes and sets imgArrow to Nothing. Then it breaks on imgArrow.Visible = True
 
hmmm...is dlcategories the second datalist?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Nope, it's the outer one. I had tried this too, with no luck:

Code:
 PanelProd = CType(Me.dlCategories.FindControl("PanelProd"), System.Web.UI.WebControls.Panel)
        dlProd = CType(PanelProd.FindControl("dlProd"), DataList)
        imgArrow = CType(dlProd.FindControl("imgArrow"), System.Web.UI.WebControls.Image)
        imgArrow.Visible = True

 
i don't know where else to go from here...what is the button clicks purpose...? just to show the images?

so originally, the image is in the datalist, with an image source set, but just not visible?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Here's what I'm trying to do:

I have a list of categories. When a category is clicked on, a list of corresponding products appears beneath the category. When a product is clicked on, its product info is displayed. I'd like for when the product is clicked on to display an arrow, indicating to the user which item they are viewing.

Everything works, except for the arrow image display...

It's not crucial that I get it working, so I may just drop it.

Thanks for your help though.
 
well if the button works to display the product, you should be able to make the image display...

is the button to display the product in the second datalist? if so post that entire button's code.

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
I think I posted it all, but here it is again.

Here's the code behind:
Code:
Public Sub ProductDetailClicked(ByVal sender As Object, ByVal e As EventArgs)

        PanelMainProducts.Visible = False
        PanelProduct.Visible = True
        myConnection = New SqlConnection(conString)
        myConnection.Open()

        mycommand = New SqlCommand("getSpecificProduct", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

        Dim objButton As LinkButton

        If Session("ProdID") = "" Then
            objButton = CType(sender, LinkButton)
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = objButton.CommandArgument
        Else
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = sender
        End If

        myReader = mycommand.ExecuteReader

        Me.Bind_Data(myReader)

        myConnection.Close()

        Dim imgArrow As System.web.UI.WebControls.Image
        Dim i As Integer

        Do While i < Me.dlCategories.Items.Count

            imgArrow = CType(Me.dlProd.Items(i).FindControl("imgArrow"), System.Web.UI.WebControls.Image)
            imgArrow.Visible = True
            i = i + 1
        Loop

    End Sub

Here's the aspx:
Code:
	<!-- Begin Category Listing -->
									<asp:datalist id="dlCategories" runat="server">
										<ItemTemplate>
											<P class="wht_14" style="MARGIN: 8px 18px 2px 0px" align="right"><b>
													
													<asp:LinkButton id="Linkbutton1" Runat="server" CommandArgument='<%# Container.DataItem("categoryID") %>' CommandName="Edit">
														<%#Container.DataItem("CategoryName")%>
													</asp:LinkButton></b>
													</P>
											<!-- Begin Product Listing -->
											<asp:Panel id="PanelProd" runat="server" Visible="false">
												<asp:DataList ID="dlProd" Runat="server">
													<ItemTemplate>
														<P class="wht_11" style="MARGIN: 0px 26px 4px 0px" align="right">
														<asp:Image ID="imgArrow" Runat="server" Visible="False" ImageUrl="/images/ico_arrowRight.gif"></asp:Image>
														<asp:LinkButton id="linkButton2" Runat="server" OnClick="ProductDetailClicked" CommandArgument='<%# Container.DataItem("productsID") %>'>
																<%#Container.DataItem("ProductName")%>
															</asp:LinkButton></P>
													</ItemTemplate>
												</asp:DataList>
												
											</asp:Panel>
											<!-- End Product Listing -->
										</ItemTemplate>
									</asp:datalist>
									<!-- End Category Listing -->
 
I think it's probably not possible or very complicated to do this, since it would be necessary to pass a specific id for the arrow image....Anyway, I'm dropping this idea. Instead, I'd like to just change the style of the selected item when it is clicked on, like make it bold or underlined or a different color...

I notice that most attributes are readonly. Some that are not are: objbutton.Forecolor, or BorderColor, but when I try to set these, I get syntax errors. What is the way to set a Forecolor?
 
Ok, so I figured out how to change the color of the selected item. Next issue: How to change it back to its original color once a different item is clicked?
Off the bat I'm thinking of looping through each item in the list and settings each forecolor to the original, then set the selected forecolor to red...Make sense?

Code:
 objButton.ForeColor = System.Drawing.Color.Red
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top