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

Hide Hyperlink Column Image

5 Answers 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 2
Allan asked on 19 Mar 2012, 08:46 PM
I add an image to my hyperlink column from the code behind. Unfortunately it adds the image even if no value for the field is in the database. I would like to hide the image if the value is NULL or empty.

Here is my ASPX column code:

<telerik:GridHyperLinkColumn DataNavigateUrlFields="Email" HeaderText=" "
     DataNavigateUrlFormatString="mailto:{0}"
     UniqueName="Email" AllowFiltering="False" ItemStyle-HorizontalAlign="Center">
 <ItemStyle Width="50px"/>
</telerik:GridHyperLinkColumn>


I have tried:
Private Sub RadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid2.ItemDataBound
    If (TypeOf e.Item Is GridDataItem) Then
 
        Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
        Dim hyperlink As HyperLink = CType(dataItem("Email").Controls(0), HyperLink)
        hyperlink.ImageUrl = "../../../images/iconoutlook.gif"
 
        If (dataItem.OwnerTableView.DataKeyValues(dataItem.ItemIndex)("Email") Is System.DBNull.Value) Then
            Dim ltrl As LiteralControl = New LiteralControl(hyperlink.Text)
            dataItem("Email").Controls.Clear()
            dataItem("Email").Controls.Add(ltrl)
        End If
 
    End If
End Sub


Any help appreciated.

5 Answers, 1 is accepted

Sort by
0
Allan
Top achievements
Rank 2
answered on 19 Mar 2012, 08:59 PM
I also tried this:
Protected Sub RadGrid2_ItemDataBound1(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid2.ItemDataBound
 
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim hyplnk As HyperLink = DirectCast(item("Email").Controls(0), HyperLink)
        Dim value As String = item("Email").Text
        If value <> " " Then
            hyplnk.Visible = False
        Else
            hyplnk.ImageUrl = "../../../images/iconoutlook.gif"
        End If
    End If
 
End Sub
0
Casey
Top achievements
Rank 1
answered on 19 Mar 2012, 09:39 PM
Hi Allan,

Are you able to see what the 'value' variable contains when the ItemDataBound hits your If statement? Although, your If statement is saying "If the value is NOT " " then hide the hyperlink, and if it is = " ", set the ImageURL". Maybe you have those backwards :).

I hope this helps!
Casey
0
Allan
Top achievements
Rank 2
answered on 19 Mar 2012, 10:12 PM
Casey,

Thanks for your reply. I tried your suggestion but no go.

Protected Sub radgrid2_itemdatabound1(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid2.ItemDataBound
 
    If TypeOf e.item Is griddataitem Then
        Dim item As griddataitem = DirectCast(e.item, griddataitem)
        Dim hyplnk As hyperlink = DirectCast(item("email").controls(0), hyperlink)
        Dim value As String = item("email").Text
        If value <> " " Then
            hyplnk.ImageUrl = "../../../images/iconoutlook.gif"
        Else
            hyplnk.Visible = False
        End If
    End If
 
End Sub
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Mar 2012, 07:05 AM
Hello Allan,

From your code I can see that you are trying to access the HyperLinkColumn's text directly using its UniqueName. In order to get the HypreLink text value, first access the control and then get the Text as shown below.
VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim itm As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim link As HyperLink = DirectCast(itm("Gender").Controls(0), HyperLink)
        If link.Text = "" Then
            link.Visible = False
        Else
            link.ImageUrl = "~/Images/img.gif"
        End If
    End If
End Sub

Thanks,
Shinu.
0
Allan
Top achievements
Rank 2
answered on 28 Mar 2012, 06:40 PM
Shinu,

You gave the best answer so far. However the GridHyperLinkColumn MUST include DataTextField= which is not added by default when creating the GridHyperLinkColumn. Once I added this, the code worked.

Here is my final code:

ASPX
<telerik:GridHyperLinkColumn
DataNavigateUrlFields="Website"
HeaderText="Website"
DataTextField="Website"
DataNavigateUrlFormatString="http://{0}"
UniqueName="Website" AllowFiltering="False"
ItemStyle-HorizontalAlign="Center">
<ItemStyle Width="50px" CssClass="radgrid" />
</telerik:GridHyperLinkColumn>

VB
Protected Sub rgd_VendorList_ItemDataBound1(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgd_VendorList.ItemDataBound
 
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim hyplnk As HyperLink = DirectCast(item("Website").Controls(0), HyperLink)
        If hyplnk.Text = "" Then
            hyplnk.Visible = False
        Else
            hyplnk.ImageUrl = "../../../images/iconIE.gif"
            hyplnk.Target = "_blank"
        End If
    End If
 
End Sub

Thank you everyone.






Tags
Grid
Asked by
Allan
Top achievements
Rank 2
Answers by
Allan
Top achievements
Rank 2
Casey
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or