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

[Solved] Adding hyperlink to grid column at runtime

3 Answers 623 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kerry
Top achievements
Rank 1
Kerry asked on 29 Jun 2009, 04:25 AM
Hello, I have RadGrid which is populated from a data table and the columns for the grid are Autogenerated...since business logic prevents me from defining a telerik:GridHyperLinkColumn at design time how can I add a Hyperlink to a column that was auto generated. I would like it to appear like a design time telerik:GridHyperLinkColumn column where the columns data's text is hyperlinked  I also trap the grids ItemDataBound event and build the hyperlink dynamically using the data from other columns in the grid.

Thanks

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Jun 2009, 08:06 AM
Hi,

Try the following code snippet in the PreRender event

protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            HyperLink link = new HyperLink();
            link.Text = item["ColumnUniqueName"].Text;
            link.NavigateUrl = "http://www.google.com";
            item["ColumnUniqueName"].Controls.Add(link);
        }
    }

Thanks,
Princy
0
Kerry
Top achievements
Rank 1
answered on 29 Jun 2009, 03:55 PM
Thanks you your suggestion, however I did make one change instead of the foreach loop on the Pre_Render event I executed your suggestion on the ItemDataBound event and used if (e.Item is GridDataItem) then executed the block of code to set the hyperlink.

If you see any issue performance or otherwise with the block below feel free to indicate so...Thanks

 

 

 

 

if (e.Item is GridDataItem)

 

{

 

 

 

 

GridDataItem dataBoundItem = (GridDataItem)e.Item;

 

 

 

 

sWorkflowInstanceID = dataBoundItem[

"WorkflowInstanceID"].Text;

 

sWorklistID = dataBoundItem[

"WorklistID"].Text;

 

sActivityRefID = dataBoundItem[

"ActivityRefID"].Text;

 

sQueueType = dataBoundItem[

"QueueType"].Text;

 

sMOSSID = dataBoundItem[

"MOSSID"].Text;

 

 

string sURL = String.Format("{3}.aspx?ActivityRefID={0}&InstanceID={1}&WorkListID={2}&MOSSOrderID={4}", sActivityRefID, sWorkflowInstanceID, sWorklistID, sQueueType, sMOSSID);

 

 

 

 

 

HyperLink link = new HyperLink();

 

link.Text = dataBoundItem[

"Order"].Text;

 

link.NavigateUrl = sURL;

dataBoundItem[

"Order"].Controls.Add(link);

 

}

0
Stuart Hemming
Top achievements
Rank 2
answered on 22 Jul 2009, 02:57 PM
I'm doing something very similar to Kerry but am hitting a small problem.

If I'm displaying a simple grid, the posted solution works fine, but any sort of interaction causes the link to be replaced with the underlying text. So, for example, a click on a row that causes an alert dialog to popup causes the link to be lost, similarly, if the row has a child table under it, clicking on the expand column causes the link to disappear.

And it's not just the link of the row I'm interacting with, but all the links on the grid being displayed.

If I cause the grid to rebind they come back.

Any thoughts?

--
Stuart
Tags
Grid
Asked by
Kerry
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kerry
Top achievements
Rank 1
Stuart Hemming
Top achievements
Rank 2
Share this question
or