Image on Hyperlink Column in Datagrid

Last post 10-31-2008 3:06 PM by bootzilla. 10 replies.

Sort Posts:

  • Image on Hyperlink Column in Datagrid

    10-29-2008, 2:34 PM
    • Member
      38 point Member
    • bootzilla
    • Member since 09-02-2008, 9:46 AM
    • Posts 343

    Does anyone know how to get an image in a hyperlink column in a datagrid? Or is it even possible?

     

  • Re: Image on Hyperlink Column in Datagrid

    10-29-2008, 4:20 PM
    • Participant
      944 point Participant
    • acroitoriu
    • Member since 08-07-2008, 12:04 PM
    • Limassol
    • Posts 160

     Hi,

     

    Try setting the HeaderText property to something like this:

    <asp:HyperLinkField HeaderText="<img src='myimage.jpg'/>" .... 

     

    Andrei

    http://acroitoriu.spaces.live.com
    "No complexity beyond what is necessary"
  • Re: Image on Hyperlink Column in Datagrid

    10-29-2008, 5:28 PM
    • Participant
      894 point Participant
    • mckrecker
    • Member since 10-24-2008, 2:07 PM
    • Posts 170

     DataGrid or GridView?

    Also, do you want the image in teh DataRow or the HeaderRow?

    If you want an image to be used for a link, I think you'll have to use a template field.

    If a DataGrid, try this:

        <asp:DataGrid runat="server" ID="DG1">
        <Columns>
        <asp:HyperLinkColumn HeaderImageUrl='<%# Eval("someImagePath") %>' DataNavigateUrlField='<%# Eval("someNaviagteURL") %>' DataTextField='<%# Eval("someText") %>' />
        <asp:TemplateColumn>
        <ItemTemplate>
        <asp:HyperLink runat="server" ID="HL1" ImageUrl='<%# Eval("someImagePath") %>' Text='<%# Eval("someText") %>' NavigateUrl='<%# Eval("someNaviagteURL") %>' />
        </ItemTemplate>
        </asp:TemplateColumn>
        </Columns>
        </asp:DataGrid>

    A GridView is essentially the same, only it would be <asp:HyperLinkField> and <asp:TemplateField>

    please mark as an answer if my reply helped
  • Re: Image on Hyperlink Column in Datagrid

    10-30-2008, 10:53 AM
    • Member
      38 point Member
    • bootzilla
    • Member since 09-02-2008, 9:46 AM
    • Posts 343

     Hi,

     

    "Try setting the HeaderText property to something like this:

    <asp:HyperLinkField HeaderText="<img src='myimage.jpg'/>" ....  "

     

    I am wanting to use the image for the item or data and not the header.

     

    Thanks

  • Re: Image on Hyperlink Column in Datagrid

    10-30-2008, 11:36 AM
    • Participant
      894 point Participant
    • mckrecker
    • Member since 10-24-2008, 2:07 PM
    • Posts 170

    Then try this:

         <asp:TemplateColumn>
        <ItemTemplate>
        <asp:HyperLink runat="server" ID="HL1" ImageUrl='<%# Eval("someImagePath") %>' Text='<%# Eval("someText") %>' NavigateUrl='<%# Eval("someNaviagteURL") %>' />
        </ItemTemplate>
        </asp:TemplateColumn>

    or, for GridView

        <asp:TemplateField>
        <ItemTemplate>
        <asp:HyperLink runat="server" ID="HL1" ImageUrl='<%# Eval("someImagePath") %>' Text='<%# Eval("someText") %>' NavigateUrl='<%# Eval("someNaviagteURL") %>' />
        </ItemTemplate>
        </asp:TemplateField>

    The "Evals" will get the data from your data source and allow you to use an image with the HyperLink.

    please mark as an answer if my reply helped
  • Re: Image on Hyperlink Column in Datagrid

    10-30-2008, 12:41 PM
    • Member
      2 point Member
    • grkshines
    • Member since 10-30-2008, 3:27 PM
    • Posts 1
    Convert the bound column to a template column, using the property builder in the VS.NET IDE (or do it yourself in the HTML of the aspx page). Then you can add an image control to the template column (to edit the template column in the IDE, right click on it and choose the appropriate option (Edit Templates I think)). To get the flag image, you could write a small function in your codebehine, make sure its protected so the aspx page can access it. Call this function from a databinding statement in the datagrid template column for the country, in the image control - something like . (This should go in the ImageURL part of the asp.net image control - i.e. ImageURL="").
  • Re: Image on Hyperlink Column in Datagrid

    10-30-2008, 2:11 PM
    • Member
      38 point Member
    • bootzilla
    • Member since 09-02-2008, 9:46 AM
    • Posts 343

    grkshines:
    Convert the bound column to a template column, using the property builder in the VS.NET IDE (or do it yourself in the HTML of the aspx page). Then you can add an image control to the template column (to edit the template column in the IDE, right click on it and choose the appropriate option (Edit Templates I think)). To get the flag image, you could write a small function in your codebehine, make sure its protected so the aspx page can access it. Call this function from a databinding statement in the datagrid template column for the country, in the image control - something like . (This should go in the ImageURL part of the asp.net image control - i.e. ImageURL="").

     

    THe problem I am having is I want to use the image as a link and I can't do that in a template column or at lease I don't think.  I have an id field as a datanavigateurlfield and that is why I am using a hyperlinkcolumn but I can't use an ImageURL in a Hyperlink field. In a template column I can't use a urlfield to link to the image. As you can see in the code below. I have a template column that is not a link(which is how it should be) and a hyperlink column with no image.

    This is the code I have:

    <asp:DataGrid ID="PortProvGrid" GridLines="Both" runat="server" AutoGenerateColumns="False"

    Visible="True" Width="728px">

    <Columns>

     

    <asp:BoundColumn DataField="facility" HeaderText="Facility" HeaderStyle-Font-Bold="true" ItemStyle-Wrap="false"></asp:BoundColumn>

    <asp:BoundColumn DataField="distance" HeaderText="Distance" HeaderStyle-Font-Bold="true" ItemStyle-Wrap="false"></asp:BoundColumn>

    <asp:TemplateColumn ItemStyle-HorizontalAlign="Center" >

    <ItemTemplate >

    <asp:HyperLink ID="hlThumbnails" ImageUrl='<%# "images/" & cstr(DataBinder.Eval(Container.DataItem, "Network_Logo_1"))%>' runat="server"></asp:HyperLink>

    </ItemTemplate>

    </asp:TemplateColumn>

    <asp:HyperLinkColumn DataNavigateUrlField="ID" DataNavigateUrlFormatString="javascript:var w=window.open('authorizationform.aspx?ID={0}','');"

    Text="Auth Form"></asp:HyperLinkColumn>

     

     

    </Columns>

    <PagerStyle BackColor="#990033" Font-Bold="True" Font-Names="Verdana" Font-Size="Smaller"

    ForeColor="White" HorizontalAlign="Right" NextPageText="Next Page" Position="TopAndBottom"

    PrevPageText="Previous Page" VerticalAlign="Middle" />

    </asp:DataGrid>

     

     

  • Re: Image on Hyperlink Column in Datagrid

    10-30-2008, 2:42 PM
    • Participant
      894 point Participant
    • mckrecker
    • Member since 10-24-2008, 2:07 PM
    • Posts 170

     Try changing the HyperLink in the Template to this:

     <asp:TemplateColumn ItemStyle-HorizontalAlign="Center" >

    <ItemTemplate >

    <asp:HyperLink ID="hlThumbnails" NavigateUrl='<%# "javascript:var w=window.open('authorizationform.aspx?ID=" & cstr(DataBinder.Eval(Container.DataItem, "ID")) & "'"%>' ImageUrl='<%# "images/" & cstr(DataBinder.Eval(Container.DataItem, "Network_Logo_1"))%>' runat="server"></asp:HyperLink>

    </ItemTemplate>

    </asp:TemplateColumn>

    In other words, do something very similar to what you did with the ImageUrl, but in the NavigateUrl and using the ID field.  Try that and see if it works.

     

    please mark as an answer if my reply helped
  • Re: Image on Hyperlink Column in Datagrid

    10-30-2008, 3:10 PM
    • Member
      38 point Member
    • bootzilla
    • Member since 09-02-2008, 9:46 AM
    • Posts 343

    mckrecker:

     Try changing the HyperLink in the Template to this:

     <asp:TemplateColumn ItemStyle-HorizontalAlign="Center" >

    <ItemTemplate >

    <asp:HyperLink ID="hlThumbnails" NavigateUrl='<%# "javascript:var w=window.open('authorizationform.aspx?ID=" & cstr(DataBinder.Eval(Container.DataItem, "ID")) & "'"%>' ImageUrl='<%# "images/" & cstr(DataBinder.Eval(Container.DataItem, "Network_Logo_1"))%>' runat="server"></asp:HyperLink>

    </ItemTemplate>

    </asp:TemplateColumn>

    In other words, do something very similar to what you did with the ImageUrl, but in the NavigateUrl and using the ID field.  Try that and see if it works.

     

     

    I don't know if the NavigateURL will work. I have tried several different ways and I get 'The server tag is not well formed.' error.

  • Re: Image on Hyperlink Column in Datagrid

    10-30-2008, 4:00 PM
    Answer
    • Participant
      894 point Participant
    • mckrecker
    • Member since 10-24-2008, 2:07 PM
    • Posts 170

     Then instead of doing it inline, use the code behind.

     After I sent that reply I tried it and got a the same problem.  Rather than figure out the right way form the tag, I swithced to using the code behind and it worked perfectly.

    To the DataGird, add the following

    <asp:DataGrid OnItemDataBound="PortProvGrid_ItemDataBound" ID="PortProvGrid" GridLines="Both" runat="server" AutoGenerateColumns="False" Visible="True" Width="728px">

    In the code behind, if using C#, add this:

      protected void gv2_RowDataBound(object sender, DataGridItemEventArgs e)
      {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //this makes sure we only apply this to rows that have data
        {
          DataRowView drv = (DataRowView)e.Item.DataItem;
          HyperLink hl = (HyperLink)e.Item.FindControl("hlThumbnails");
          hl.NavigateUrl="javascript:var w=window.open('authorizationform.aspx?ID=" + drv["ID"].ToString() + "')";
        }
      }

    If you're using VB, then search MSDN for "DataGridItem ItemDataBound" for the equivalent: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagriditem.dataitem.aspx

    ALSO -- please note I use GridViews mostly, not datagrids, so I tried to make sure I got the syntax right for datagrids, but may have made a mistake. The above worked for me using a grdiview, so it should work with some modifications to a datagrid.

    please mark as an answer if my reply helped
  • Re: Image on Hyperlink Column in Datagrid

    10-31-2008, 3:06 PM
    • Member
      38 point Member
    • bootzilla
    • Member since 09-02-2008, 9:46 AM
    • Posts 343

    mckrecker:

     Then instead of doing it inline, use the code behind.

     After I sent that reply I tried it and got a the same problem.  Rather than figure out the right way form the tag, I swithced to using the code behind and it worked perfectly.

    To the DataGird, add the following

    <asp:DataGrid OnItemDataBound="PortProvGrid_ItemDataBound" ID="PortProvGrid" GridLines="Both" runat="server" AutoGenerateColumns="False" Visible="True" Width="728px">

    In the code behind, if using C#, add this:

      protected void gv2_RowDataBound(object sender, DataGridItemEventArgs e)
      {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //this makes sure we only apply this to rows that have data
        {
          DataRowView drv = (DataRowView)e.Item.DataItem;
          HyperLink hl = (HyperLink)e.Item.FindControl("hlThumbnails");
          hl.NavigateUrl="javascript:var w=window.open('authorizationform.aspx?ID=" + drv["ID"].ToString() + "')";
        }
      }

    If you're using VB, then search MSDN for "DataGridItem ItemDataBound" for the equivalent: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagriditem.dataitem.aspx

    ALSO -- please note I use GridViews mostly, not datagrids, so I tried to make sure I got the syntax right for datagrids, but may have made a mistake. The above worked for me using a grdiview, so it should work with some modifications to a datagrid.

    Sorry for the delay. This worked.

     

    Thanks

Page 1 of 1 (11 items)