Conditional Hyperlink in radgrid
1 Answer 16 Views
Itamar
Top achievements
Rank 1
Itamar asked on 11 May 2021, 01:49 PM

Hi,

I have  a Gridhyprlink column with a DataNavigateUrlFormatString and 2    DataNavigateUrlFields

however I want one of those fileds to be conditional on another Column;

eg

DataNavigateUrlFormatString="https://main.aspx?etn={0}&pagetype=entityrecord&id={1}"

 DataNavigateUrlFields= ((if (entityid ==2) {a} else {b}) ,mainid )

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 14 May 2021, 10:39 AM

Hi Itamar,

When working with items dynamically/conditionally you will need to that in the backend programmatically.

Create a GridHyperLinkColumn with a HeaderText and a ColumnName.

<telerik:GridHyperLinkColumn HeaderText="HyperLink Column" UniqueName="HyperLinkColumn">
</telerik:GridHyperLinkColumn>

 

Attach the ItemDataBound event to the Grid and in that event, you can access any values, controls. So you can access different columns in the Grid, get the values of those cells, and format the HyperLink control of the GridHyperLinkColumn.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        var dataItem = (GridDataItem)e.Item;

        var myHyperLink = dataItem["HyperLinkColumn"].Controls[0] as HyperLink;

        myHyperLink.Text = "View Record";

        var mainid = (int)dataItem.GetDataKeyValue("mainid");

        // Your condition which returns a different column uniquename based on the results
        var conditionalField = entityid == 2 ? "Column1" : "Column2";

        // access the text of that specific column
        var conditionalValue = dataItem[conditionalField].Text;

        myHyperLink.NavigateUrl = string.Format("https://main.aspx?etn={0}&pagetype=entityrecord&id={1}", conditionalValue, mainid);
    }
}

 

For more details, you can refer to the following articles:

 

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Thank you exactly what I needed ,There is still one issue however I have button that hides the column but when i reshow it the data dissapears any way I can fix this thanks
- by
Itamar
Top achievements
Rank 1
on 18 May 2021, 11:04 AM
For hiding the columns you can check out the how to hide columns in radgrid forum thread. Regarding the data that gets disappeared, it is likely caused by incorrect data binding. Make sure you bind data to RadGrid either using Programmatic Data Binding Using the NeedDataSource Event or Declarative DataSource, see How to bind RadGrid properly on server-side
- by
Attila Antal
Telerik team
on 19 May 2021, 03:54 PM
Thanks alot I managed to do it by attaching it to the ItemCreated Event instead of the ItemBound Event
- by
Itamar
Top achievements
Rank 1
on 20 May 2021, 09:24 AM
Asked by
Itamar
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or