HyperLinkColumn contents disappear when Grid cycles in/out of view in RadMultiPage

2 posts, 1 answers
  1. ColinBowern
    ColinBowern avatar
    51 posts
    Member since:
    Aug 2012

    Posted 03 Jun 2010 Link to this post

    I have a grid which is contained in one of the tabs of a RadTabStrip/RadMultiPage setup.  Each of the tabs causes an AJAX postback to dynamically load the contents upon first click of that tab.  There is a grid on the first tab which is loaded when the page is displayed.  The contents of the HyperLinkColumn disppear if I use the following code in the grid's ItemDataBound event:

    if (e.Item is GridDataItem) 
        var item = e.Item as GridDataItem; 
        var name = (HyperLink)item["Name"].Controls[0]; 
     
        var extension = Path.GetExtension(name.Text).TrimStart('.'); 
        var fileIcon = new HtmlGenericControl("span"); 
        if (SiteMaster.FileIconCssClass.ContainsKey(extension)) 
        { 
            fileIcon.Attributes.Add("class""FileIcon " + SiteMaster.FileIconCssClass[extension]); 
        } 
        else 
        { 
            fileIcon.Attributes.Add("class""FileIcon FileIcon-Default"); 
        } 
        item["Name"].Controls.AddAt(0, fileIcon); 


    When I comment out the code block it works fine, however.  Any thoughts on why injecting an extra span into the HyperLinkColumn causes this behavior?
  2. Answer
    Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 08 Jun 2010 Link to this post

    Hi Colin,

    The reason for hyperlinks to disappear is the incorrect loading of controls. As you may know ItemDataBound event is raised only when the grid control is created from the datasource and not when its structure is re-created from the ViewState. Therefore adding controls in this event will result in construction of a different control tree on the next  postback. In order to correctly load controls in your scenario you should use ItemCreated instead and populate them in ItemDataBound if needed. For example:

    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            var item = e.Item as GridDataItem;
            var name = (HyperLink)item["Name"].Controls[0];            
            var fileIcon = new HtmlGenericControl("span");            
            item["Name"].Controls.AddAt(0, fileIcon);
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            var item = e.Item as GridDataItem;
            var fileIcon = (HtmlGenericControl)item["Name"].Controls[0];
              
            //control value modifications....
        }
    }

    Best wishes,
    Rosen
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top