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

GridHyperLinkColumn - when user doesn't have permission to view URL I need OnItemCommand to fire when the cell is clicked?

4 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 09 Jun 2015, 01:50 PM

Hi,

I have been Googling for a solution but I haven't found one. What I need to do is relatively simple I think. I am using a GridHyperLinkColumn. Sometimes the user will not have permission to view a particular link though.

So, in the code behind what I have at the moment is something like the code below. What I need to fill in is the what goes here ?????? bit. Is it possible to populate NavigateUrl with something that will make OnItemCommand fire in the normal way?

protected void OnItemDataBound(object sender, GridItemEventArgs e)
{
    var gridDataItem = e.Item as GridDataItem;
 
    if (gridDataItem != null)
    {
        var entity = gridDataItem.DataItem as MyEntity;
          
        if (entity != null)
        {
            if (CanViewThisEntity)
            {
                var url = GetLinkForEntitySomehow();
                var link = (HyperLink)gridDataItem["Name"].Controls[0];
 
                if (!string.IsNullOrEmpty(url))
                {
                    link.NavigateUrl = url;
                }
            }
            else
            {
                      what goes here ??????
            }

My problem at the moment is that OnItemCommand will fire only if the cell is clicked somewhere outside the text of the anchor tag (because the anchor is being rendered without a link).Or is there a better way maybe. I hope I am making sense! 

Thanks.

 

4 Answers, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 12 Jun 2015, 01:16 PM
Hi John,

You can use the following approach to achieve this requirement:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        TableCell cell = item["ShipCountry"];
        HyperLink link = cell.Controls[0] as HyperLink;
        link.Visible = false;
        cell.Controls.Add(new LiteralControl(link.Text));
    }
}

Hope this helps. Please give it a try and let me know if it works for you.

You can also achieve this functionality using GridTemplateColumn. I am sending a sample RadGrid web site to demonstrate various ways of creating link columns.

Regards,
Eyup
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 18 Jun 2015, 10:26 AM
Thanks very much. I tried exactly that and it worked perfectly :)
0
John
Top achievements
Rank 1
answered on 28 Jan 2016, 03:32 PM

Hello. This problem has come up for me again. I have solved it in a different way. I thought I would share the solution.

It's very simple. Use a GridTemplateColumn as below and show/hide the Label or the LinkButton as needed. 

<telerik:GridTemplateColumn DataField="FooName" HeaderText="Foo Name" UniqueName="FooName"
    SortExpression="Enabled" AllowSorting="True">
    <ItemTemplate>
        <asp:Label ID="noLinkLabelFoo" runat="server"/>
        <asp:LinkButton ID="linkButtonFoo" runat="server"/>
    </ItemTemplate>
</telerik:GridTemplateColumn>

 The appropriate control can be shown/hidden in the OnItemDataBound method as follows.

protected void GridOnItemDataBound(object sender, GridItemEventArgs e)
{
    var gridDataItem = e.Item as GridDataItem;
     
    var whatever = e.Item.DataItem as Whatever;
 
    if (gridDataItem != null)
    {
        var noLinkLabel = (Label)gridDataItem["noLinkLabelFoo"].FindControl("noLinkLabelID");
        var linkButton = (LinkButton)gridDataItem["linkButtonFoo"].FindControl("linkButtonID");
 
        bool showHyperLink = !string.IsNullOrEmpty(whatever.URL);
 
        if (linkButton != null && noLinkLabel != null)
        {
            linkButton.Enabled = showHyperLink;
            linkButton.Visible = showHyperLink;
            noLinkLabel.Enabled = !showHyperLink;
            noLinkLabel.Visible = !showHyperLink;
 
            linkButton.Text = linkText;
            noLinkLabel.Text = linkText;
            linkButton.PostBackUrl = url;
        }
    }
}

I hope that somebody finds this useful.

0
Eyup
Telerik team
answered on 02 Feb 2016, 01:04 PM
Hello John,

Thank you for sharing your approach with our community.
I hope it will prove helpful to other developers as well.

Regards,
Eyup
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Eyup
Telerik team
John
Top achievements
Rank 1
Share this question
or