How can we add hyperlink column in RadGrid with Image

3 posts, 0 answers
  1. KS
    KS avatar
    44 posts
    Member since:
    Jul 2010

    Posted 20 Jul 2010 Link to this post

    I have requirement to add Image in the RadGrid as Hyperlink

    Code-Behind is like this

     Protected Sub radgrid1_ItemDataBound(ByVal source As Object, ByVal e As GridItemEventArgs)

                If (e.Item.ItemType = GridItemType.Item) Then

                    Dim HtmlImg As New System.Web.UI.WebControls.Image
                    HtmlImg = CType(e.Item.FindControl("ImgMachine"), System.Web.UI.WebControls.Image)

                    Dim ProductId As New HiddenField
                    ProductId = CType(e.Item.FindControl("hdnOrderNumber"), HiddenField)

                    HtmlImg.Attributes.Add("onclick", "javascript:window.open('http://www.myshoppingCart.no/OpenShopPackageTracker.aspx?installationID=21140000023&orderNumber=" & ProductId.Value.ToString() & "'); return false;")


                End If

            End Sub

    Please help me with inline code for this
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Jul 2010 Link to this post

    Hello Kalit,

    One solution is to use one GridTemplateColumn with Image in ItemTemplate instead of HyperLinkColumn. Then from code behind attach 'onclick' client event to the Image. Check out the following code snippet.

    ASPX:
    <telerik:GridTemplateColumn>
         <ItemTemplate>
             <asp:Image ID="Image1" runat="server" />
         </ItemTemplate>
     </telerik:GridTemplateColumn>

    VB.Net:
    Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim img As Image = DirectCast(item.FindControl("Image1"), Image)
            Dim ProductId As New HiddenField()
            ProductId = DirectCast(e.Item.FindControl("hdnOrderNumber"), HiddenField)
            img.Attributes.Add("onclick", "javascript:window.open('http://www.myshoppingCart.no/OpenShopPackageTracker.aspx?installationID=21140000023&orderNumber=" + ProductId.Value.ToString() & "'); return false;")
        End If
    End Sub

     Another suggestion is directly attaching 'onclick' event to the Image by passing 'OrderNumber'(i think it is one of DataField in your DB) to the event handler.

    ASPX:
    <telerik:GridTemplateColumn>
       <ItemTemplate>
           <img alt="" src="../Images/Cancel.gif" onclick='openWindow(<%#Eval("OrderNumber") %>);' />
       </ItemTemplate>
    </telerik:GridTemplateColumn>

    Java Script:
    <script type="text/javascript">
        function openWindow(value)
        {
          window.open(url);
        }
    </script>

    Thanks,
    Princy.
  3. Santodip
    Santodip avatar
    2 posts
    Member since:
    Jul 2010

    Posted 28 Sep 2010 Link to this post

    Thanks ............... its really helpful
Back to Top