Hide Hyperlink Column Image

6 posts, 1 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 19 Mar 2012 Link to this post

    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.
  2. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 19 Mar 2012 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 19 Mar 2012 Link to this post

    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
  5. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 19 Mar 2012 Link to this post

    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
  6. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Mar 2012 Link to this post

    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.
  7. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 28 Mar 2012 Link to this post

    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.






Back to Top
UI for ASP.NET Ajax is Ready for VS 2017