RadTreelist -Iterate Item

4 posts, 0 answers
  1. Sindu
    Sindu avatar
    46 posts
    Member since:
    Oct 2011

    Posted 03 Nov 2011 Link to this post


    Hi,

    My requirement is to dispaly a hyperlink for some words in RadTreelist.
    ie is I have a Column 'Description' , this column contains words like 

    " Telerik is a good tool ."  I need to display <good> in hyperlink.

    How is it possible in RadTreeList after binding the data?
    which event is used for that purpose?

    Thanks,
    Sindu.
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 03 Nov 2011 Link to this post

    Hello,

    <telerik:RadTreeList runat="server" ID="RadTreeList1" AutoGenerateColumns="false"
               DataKeyNames="ID" ParentDataKeyNames="parentID">
               <Columns>
                     <telerik:TreeListBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID" />
                     <telerik:TreeListBoundColumn DataField="parentID" UniqueName="parentID" HeaderText="parentID" />
                     <telerik:TreeListBoundColumn DataField="Description" UniqueName="Description" HeaderText="Description" />
                     <telerik:TreeListTemplateColumn UniqueName="DescriptionwithLink">
                       <ItemTemplate>
                           <asp:Literal ID="Literal1" runat="server"></asp:Literal>
                       </ItemTemplate>
                     </telerik:TreeListTemplateColumn>
               </Columns>
           </telerik:RadTreeList>
    protected void Page_Load(object sender, EventArgs e)
        {
            RadTreeList1.NeedDataSource += new EventHandler<TreeListNeedDataSourceEventArgs>(RadTreeList1_NeedDataSource);
            RadTreeList1.ItemDataBound += new EventHandler<TreeListItemDataBoundEventArgs>(RadTreeList1_ItemDataBound);
        }
      
        void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
        {
            if (e.Item is TreeListDataItem)
            {
                TreeListDataItem item = e.Item as TreeListDataItem;
                string strtxt = item["Description"].Text;
                Literal Literal1 = item.FindControl("Literal1") as Literal;
                strtxt = strtxt.Replace("good", "<a href='http://www.google.co.in/'> good <a>");
                Literal1.Text = strtxt;
            }
        }
      
        void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                    new { ID = 1, parentID = 0, Description="Telerik is a good tool"},
                    new { ID = 2, parentID = 1, Description="Telerik is a good tool"},
                    new { ID = 3, parentID = 1, Description="Telerik is a good tool"}
                     
                };
            RadTreeList1.DataSource = data;
        }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sindu
    Sindu avatar
    46 posts
    Member since:
    Oct 2011

    Posted 07 Nov 2011 Link to this post


    Hi jayesh ,

    I have iterate an item and set a hyperlink of particular text  using the code you were given  and also I used the TreeListBoundColumn and set link for the paricular tex. Hyperlink is coming for that paricular text ,but the text colour is not changed to blue.Usually when we set a hyperlink thye colur of that text will be changed to blue.How is it possible in treelist?

    Thanks,
    sindu.
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 07 Nov 2011 Link to this post

    Hello,

    Method 1:
    <telerik:TreeListTemplateColumn UniqueName="DescriptionwithLink">
                        <ItemTemplate>
                         <asp:Literal ID="Literal1" runat="server"></asp:Literal>
                        </ItemTemplate>
                    </telerik:TreeListTemplateColumn>
    ...................
     if (e.Item is TreeListDataItem)
            {
                TreeListDataItem item = e.Item as TreeListDataItem;
                string strtxt = item["Description"].Text;

    strtxt = strtxt.Replace("good", "<a  style=\"color:Blue;text-decoration:underlind;\" href='http://www.google.co.in/'> good <a>");


    Method 2:

      <telerik:TreeListTemplateColumn UniqueName="DescriptionwithLink">
                        <ItemTemplate>
                         
                        </ItemTemplate>
                    </telerik:TreeListTemplateColumn>
    ..............................
     if (e.Item is TreeListDataItem)
            {
                TreeListDataItem item = e.Item as TreeListDataItem;
                string strtxt = item["Description"].Text;
               LiteralControl lst1 = new LiteralControl();
                LiteralControl lst2 = new LiteralControl();
                HtmlAnchor a1 = new HtmlAnchor();

                lst1.Text = "Hi ";
                lst2.Text = " Goyani";
                a1.InnerText = "Jayesh";
                a1.HRef = "http://www.google.co.in/";
                a1.Style.Add("color", "Blue");

                item["DescriptionwithLink"].Controls.Add(lst1);
                item["DescriptionwithLink"].Controls.Add(a1);
                item["DescriptionwithLink"].Controls.Add(lst2);


    Thanks,
    Jayesh Goyani
Back to Top