This is a migrated thread and some comments may be shown as answers.

Appending to grid cells

1 Answer 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rich
Top achievements
Rank 1
Rich asked on 17 Oct 2012, 07:07 PM
I have a dashboard page that displays a grid.

One of the columns has a hyperlink in it.  What I would like to do, is in the pre-Render event for the grid, loop through the items and if there is a value in this column, add an <a> tag in that same cell (leaving the hyperlink alone), with an icon so that if they click on that icon the user will be redirected to another page. 

It could be a javascript function or codebehind that makes the call.

Anyone ever done something like this?  Anyone have any ideas?

Rich

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Oct 2012, 05:55 AM
Hi,

I suppose you want to add a hyperlink to the template column. In general the proper place for adding controls to the grid items is in ItemCreated. But in the case of adding controls to the cells of GridBoundColumn,you cannot use ItemCreated only, but a combination of ItemCreated and ItemDataBound. This is due to the fact that the control created in ItemCreated will be erased when data-binding this control. Also, if you create the control in ItemDataBound when the controls are created from ViewState, the grid will not raise ItemDataBound, and the control will not be created and would not raise postback events. The solution for such cases is to create the control in ItemDataBound and recreate this control if needed on ItemCreated for subsequent postbacks. Here is the sample code.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        HyperLink link = (HyperLink)item.FindControl("HyperLink1");
        if (link.Text == "text")
        {
            HyperLink link1 = new HyperLink();
            link1.Text = "new text";
            link1.NavigateUrl = "Page.aspx";
            item["template"].Controls.Add(link1);
 
        }
    }
}
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        HyperLink link = (HyperLink)item.FindControl("HyperLink1");
        if (link.Text == "text")
        {
            HyperLink link1 = new HyperLink();
            link1.Text = "new text";
            link1.NavigateUrl = "Page.aspx";
            item["template"].Controls.Add(link1);
 
        }
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Rich
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or