This is a migrated thread and some comments may be shown as answers.
HyperLinkColumn contents disappear when Grid cycles in/out of view in RadMultiPage
1 Answer 19 Views
This is a migrated thread and some comments may be shown as answers.
ColinBowern
Top achievements
Rank 1
ColinBowern asked on 03 Jun 2010, 04:23 PM
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?

1 Answer, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 08 Jun 2010, 02:12 PM
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.
Tags
Grid
Asked by
ColinBowern
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or