GridView with imagefield - want imagefield to link to dynamic URL

Last post 04-03-2008 9:31 AM by sreenatht. 3 replies.

Sort Posts:

  • GridView with imagefield - want imagefield to link to dynamic URL

    04-02-2008, 2:51 PM
    • Member
      1 point Member
    • kanangela
    • Member since 04-02-2008, 6:30 PM
    • Posts 19

    HI, I am an ASP.NET newbie. I am creating a web based employee directory for my firm. I have a GridvView with an imagefield that contains a photo of each employee.  I want to be able to let users click on the image and be linked to the details page for that employee.  I have a hyperlinkfield that contains the employee's name and links to the details page, but I want them to have the choice of clicking the name or the image.  I can't seem to figure out how to set a navigateURL on the imagefield.  There is no property for it.  I guess I will need to write some code behind?  I am including the code for the gridview below.  Thanks in advance for your help.

     <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="User_Name,Secy_UserName" DataSourceID="SqlDataSource1" Font-Names="Tahoma" Font-Size="8pt" SkinID="gridviewskin">

    <Columns>

    <asp:boundfield DataField="User_Name" HeaderText="User_Name" SortExpression="User_Name" Visible="False">

    </asp:boundfield>

    <asp:imagefield DataImageUrlField="Photo_Location" HeaderText="Photo" DataAlternateTextField="Emp_Name">

    <ControlStyle Height="48px" Width="50px" />

    </asp:imagefield>

    <asp:hyperlinkfield DataNavigateUrlFields="User_Name" DataNavigateUrlFormatString="~/directory/empdetails.aspx?User_Name={0}" DataTextField="Emp_Name" HeaderText="Name" SortExpression="Emp_Name">

    </asp:hyperlinkfield>

    <asp:boundfield DataField="Title" HeaderText="Title" SortExpression="Title">

    <ItemStyle Width="150px" />

    </asp:boundfield>

    <asp:boundfield DataField="Emp_Phone" HeaderText="Phone" SortExpression="Emp_Phone">

    </asp:boundfield>

    <asp:boundfield DataField="Emp_Fax" HeaderText="Fax" SortExpression="Emp_Fax">

    </asp:boundfield>

    <asp:hyperlinkfield DataNavigateUrlFields="Secy_UserName" DataNavigateUrlFormatString="~/directory/empdetails.aspx?User_Name={0}" DataTextField="Secy_Name" HeaderText="Secretary Name" SortExpression="Secy_Name">

    </asp:hyperlinkfield>

    <asp:boundfield DataField="Secy_Phone" HeaderText="Secretary Phone" SortExpression="Secy_Phone">

    </asp:boundfield>

    </Columns>

    <HeaderStyle HorizontalAlign="Left" />

    </asp:GridView>

  • Re: GridView with imagefield - want imagefield to link to dynamic URL

    04-02-2008, 3:16 PM
    • Member
      491 point Member
    • msnyder0
    • Member since 06-29-2007, 6:04 PM
    • PA, USA
    • Posts 226

    Have you tried setting an ImageURL on a Hyperlink? (opposite of what you were trying)

    If you'd like to try doing some manual databinding on the Page_Load, this is what I've been using...
    You'd just need to change the one line to this... CType(row.FindControl("hlEmail"), HyperLink).ImageURL = "~/Path"

    Protected Sub InitPkgRes()

    gvResources.DataBind()

    For Each row As GridViewRow In gvResources.Rows

    CType(row.FindControl("hlEmail"), HyperLink).NavigateUrl = "mailto:" & CType(row.FindControl("hlEmail"), HyperLink).Text

    CType(row.FindControl("hlEmail"), HyperLink).Text = "E-mail"

     

    Next

    End Sub

  • Re: GridView with imagefield - want imagefield to link to dynamic URL

    04-03-2008, 8:52 AM
    • Member
      1 point Member
    • kanangela
    • Member since 04-02-2008, 6:30 PM
    • Posts 19

    I'm not sure how to implement what you are suggesting.  I have created a hyperlink column to replace my imagefield.  I databinded it to User_Name on DataNavigateUrlFields and set the DataNavigateUrlFormatString to ~/directory/empdetails.aspx?User_Name={0}

    I am using Visual Studio 2005 and I have double-clicked the page from the designer to create the page_load sub.  I'm not sure if your code should go inside that or if it is separate, but I got method errors pasting it within, and putting it outside the sub tells me that gvResources is undeclared.

    I am not sure where the manual databinding is occurring, either.  This sounds a little advanced for my skill level, so any further explanation you can provide would be appreciated.

  • Re: GridView with imagefield - want imagefield to link to dynamic URL

    04-03-2008, 9:31 AM
    Answer
    • Member
      258 point Member
    • sreenatht
    • Member since 11-12-2007, 11:35 PM
    • Thrissur,Kerala,India
    • Posts 112

     You can use link button like following instead of hyperlink like following

    <temTemplate>
                 <asp:LinkButton ID="finished" runat="server" CommandArgument='<%# EVAl("employeedetails") %>' OnClick="finished_Click" ><img src="pics/more.gif" alt="" style="border:0;" /></asp:LinkButton>        
               
                </ItemTemplate>

     

    Here onclick I defined a function which You should define inside code behind like following.(see its VB code if need You can change it to C# by a converter )

    Public Sub finished_Click(ByVal sender As Object, ByVal e As EventArgs)

    //Code

    End sub

    If you are picking ID of that employee in '<%# EVAl("employeeID") %>'  by using that ID You can fetch the detalis.

     

     

    Please Mark Answer if You find Your solution 

    Thanks&Regards
    Sreenath.T
Page 1 of 1 (4 items)