Access Image in TreeView

6 posts, 1 answers
  1. Mike_T
    Mike_T avatar
    37 posts
    Member since:
    Aug 2006

    Posted 05 Feb 2011 Link to this post

    Hi Guys,
    How do i access the image value to show/hide from code behind in a treeview using the following code
    Thanks ...

    <telerik:RadTreeView runat="server" ID="RadTreeView1">
        <Nodes>
            <telerik:RadTreeNode Text="03" Expanded="True">
                <Nodes>
                    <telerik:RadTreeNode Text="4" Expanded="true">
                        <NodeTemplate>
                            <%# DataBinder.Eval(Container, "Text") %>
                        </NodeTemplate>
                        <Nodes>
                            <telerik:RadTreeNode Text="1">
                                <NodeTemplate>
                                    <img src="Images/button_download1.gif" alt="" />
                                    <%# DataBinder.Eval(Container, "Text") %>
                                </NodeTemplate>
                            </telerik:RadTreeNode>
                        </Nodes>
                    </telerik:RadTreeNode>
                    <telerik:RadTreeNode Text="1" Expanded="False">
                        <NodeTemplate>
                            <%# DataBinder.Eval(Container, "Text") %>
                        </NodeTemplate>
                        <Nodes>
                            <telerik:RadTreeNode Text="2">
                                <NodeTemplate>
                                    <img src="Images/button_download2.gif" alt="" />
                                    <%# DataBinder.Eval(Container, "Text") %>
                                    </span> </div>
                                </NodeTemplate>
                            </telerik:RadTreeNode>
                        </Nodes>
                    </telerik:RadTreeNode>
                </Nodes>
            </telerik:RadTreeNode>
        </Nodes>
    </telerik:RadTreeView>
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Feb 2011 Link to this post

    Hi Mike,


    To get a reference to the embedded control, locate the Node that has the Template, and use its FindControl server-side method.
    Accessing Controls Inside Templates

    Note: You need to add runat="server" to get reference to "img" control in code behind, otherwise use <asp:Image> control for the same purpose.


    -Shinu.
  3. Mike_T
    Mike_T avatar
    37 posts
    Member since:
    Aug 2006

    Posted 07 Feb 2011 Link to this post

    Hi,
    I changed my code to be like this but still no access to the image

    <telerik:RadTreeView runat="server" ID="RadTreeView1"
        <Nodes
            <telerik:RadTreeNode Text="03" Expanded="True"
                <Nodes
                    <telerik:RadTreeNode Text="4" Expanded="true"
                        <NodeTemplate
                            <%# DataBinder.Eval(Container, "Text") %> 
                        </NodeTemplate
                        <Nodes
                            <telerik:RadTreeNode Text="1"
                                <NodeTemplate
                                    <asp:Image src="Images/button_download.gif"  runat="server" id="img11" alt=""  /> 
                                    <%# DataBinder.Eval(Container, "Text") %> 
                                </NodeTemplate
                            </telerik:RadTreeNode
                        </Nodes
                    </telerik:RadTreeNode
                    <telerik:RadTreeNode Text="1" Expanded="False"
                        <NodeTemplate
                            <%# DataBinder.Eval(Container, "Text") %> 
                        </NodeTemplate
                        <Nodes
                            <telerik:RadTreeNode Text="2"
                                <NodeTemplate
                                    <img src="Images/button_download2.gif" alt="" /> 
                                    <%# DataBinder.Eval(Container, "Text") %> 
                                    </span> </div
                                </NodeTemplate
                            </telerik:RadTreeNode
                        </Nodes
                    </telerik:RadTreeNode
                </Nodes
            </telerik:RadTreeNode
        </Nodes
    </telerik:RadTreeView>
      
    Code Behind:
      
        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not Page.IsPostBack Then
                RadTreeView1.DataBind()
                Dim node As RadTreeNode = RadTreeView1.Nodes(0)
                Dim img11 As Image = DirectCast(node.FindControl("img11"), Image)
                img11.Visible = False
            End If
        End Sub
  4. Nikolay Tsenkov
    Admin
    Nikolay Tsenkov avatar
    734 posts

    Posted 08 Feb 2011 Link to this post

    Hi Mike_T,

    Could you, please, try getting the control by the position of it's declaration (i.e. index), e.g. node.Controls[0] and then cast it to the appropriate type.

    Also, are you sure that the node that you try to get an image from has it? Because as I see you try to get an image from the first root node, which obviously doesn't have one. Please, check that out and let me know the results.


    Regards,
    Nikolay Tsenkov
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  5. Mike_T
    Mike_T avatar
    37 posts
    Member since:
    Aug 2006

    Posted 15 Feb 2011 Link to this post

    Sorry guys,

    I'm not able to solve it can you please help?

    Thanks
  6. Answer
    Helen
    Admin
    Helen avatar
    1052 posts

    Posted 18 Feb 2011 Link to this post

    Hello Mike,

    Please use the code below:

    //aspx:
    <telerik:RadTreeView runat="server" ID="RadTreeView1">
        <Nodes>
            <telerik:RadTreeNode Text="03" Expanded="True">
                <Nodes>
                    <telerik:RadTreeNode Text="4" Expanded="true">
                        <NodeTemplate>
                            <%# DataBinder.Eval(Container, "Text") %>
                        </NodeTemplate>
                        <Nodes>
                            <telerik:RadTreeNode Text="1">
                                <NodeTemplate>
                                    <asp:Image src="Images/button_download.gif"  runat="server" id="img11" alt="button_download"  />
                                    <%# DataBinder.Eval(Container, "Text") %>
                                </NodeTemplate>
                            </telerik:RadTreeNode>
                        </Nodes>
                    </telerik:RadTreeNode>
                    <telerik:RadTreeNode Text="1" Expanded="False">
                        <NodeTemplate>
                            <%# DataBinder.Eval(Container, "Text") %>
                        </NodeTemplate>
                        <Nodes>
                            <telerik:RadTreeNode Text="2">
                                <NodeTemplate>
                                    <img src="Images/button_download2.gif" alt="" />
                                    <%# DataBinder.Eval(Container, "Text") %>
                                    </span> </div>
                                </NodeTemplate>
                            </telerik:RadTreeNode>
                        </Nodes>
                    </telerik:RadTreeNode>
                </Nodes>
            </telerik:RadTreeNode>
        </Nodes>
    </telerik:RadTreeView>


    vb:
    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
           If Not Page.IsPostBack Then
               RadTreeView1.DataBind()
               Dim node As RadTreeNode = RadTreeView1.Nodes(0).Nodes(0).Nodes(0)
               Dim img11 As Image = DirectCast(node.FindControl("img11"), Image)
               img11.Visible = False
           End If
       End Sub


    Hope this helps.

    Regards,
    Helen
    the Telerik team
Back to Top