Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HyperLinkField not working

Posted on 2007-03-21
19
Medium Priority
?
986 Views
Last Modified: 2008-01-09
I'm sure I've had this working on previous projects.
I want the column header to say photo.
I want the clickable text to say "click"
I want the URL to be the value of the field (contains the full path).
At present I have the path to the resource but is not a link. The html source shows as
<a target="_blank">click</a>

the code is
<asp:HyperLinkField DataNavigateUrlFields="photo_path" DataTextField="photo_path" Target="_blank" DataTextFormatString="click" HeaderText="Photo" />

There is data in the field becuase if i set the text to also be the datafield then rather than click i get the full path to the photo shown in the column (still not clickable)
0
Comment
Question by:QPR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18769173

 <asp:HyperLinkField DataNavigateUrlFields="photo_path" HeaderText="Photo" Text="Click" Target="_blank"  />
0
 
LVL 29

Author Comment

by:QPR
ID: 18769182
Tried that and now I just have the word "click" text not link, the html is....
<a target="_blank">Click</a>

Strange thing is that when I view it in the IDE (vs2005) it is underlined and blue but when I test or publish it is plain text
0
 
LVL 29

Author Comment

by:QPR
ID: 18769185
an example of the fields value is....
c:\inetpub\wwwroot\myApp\images\photos\Footpath.gif
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Edwin_C
ID: 18769203
It works for me.  I am using gridview and asp.net 2.0.
Try this then
<asp:HyperLinkField DataNavigateUrlFields="photo_path" DataNavigateUrlFormatString="{0}" HeaderText="Photo" Text="Click" Target="_blank"  />
0
 
LVL 29

Author Comment

by:QPR
ID: 18769208
Nope same result.
It's worked for me in the past, I'm stumped as to what is going on here
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18769223
This is because of the"\" in data.

Use

<asp:TemplateField HeaderText="Photo">
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("equipmentCode", "{0}") %>'
                            Text="Click"></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
0
 
LVL 29

Author Comment

by:QPR
ID: 18769231
Thanks, now it's a link but nothing happens when clicked.
HTML is
<a id="GridView1_ctl09_HyperLink1" href="c:\inetpub\wwwroot\MyApp\images\photos\2FrontLine.jpg">Click</a>

0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18769272
As I know, you cannot open local file from web site.  You can open networked resources like "//myserver/myfolder/myfile.ext".

I think it is better to make the folder c:\inetpub\wwwroot\MyApp\images\photos a virtual folder (in IIS admin) under your web site and then you can user path like

/PhotoFolder/2FrontLine.jpg
0
 
LVL 29

Author Comment

by:QPR
ID: 18769842
I dont have the exact code to hand (its at work) but the path is made up ot 3 things...
something like appdomain.something.something a hard coded folder "images/photos" and the name of the uploaded file. Sorry that's very vague I'lll send the proper appdomain bit when at work.
I'd much rather the link pointed to the images relative to the page rather than advertising the local disk path (C wwwroot etc).  As I said tomorrow I will post how the path is created and what ends up in the db (photo_path)
0
 
LVL 29

Author Comment

by:QPR
ID: 18777378
' Dim savePath As String = AppDomain.CurrentDomain.BaseDirectory & "\images\photos\"
 Dim savePath As String = Server.MapPath("~") & "\images\photos\"

are the 2 methods I have tried both give a path (in the db and link text) of C:\inetpub\wwwroot etc etc
I'd like for the value passed to the DB (insert parameter) and the href value to be relative URLS like you suggest   / (or ~) images/photos/image.gif
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18778954
If your photo is stored in c:\inetpub\wwwroot\MyApp\images\photos\ and c:\inetpub\wwwroot\MyApp is the root of your webs site, then the image can be refered as ~/image/photos/2FrontLine.jpg.  Can you simple add

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='~/image/photos/2FrontLine.jpg'
                            Text="Click"></asp:HyperLink>

in your webform and see if it works?


0
 
LVL 29

Author Comment

by:QPR
ID: 18789503
That works perfectly and is what I'm trying to achieve.
The URL (looking at the status bar) is relative to the page and not a file:///C/wwroot etc URL.
Now if I could get this value using NavigateUrl='<%# Eval("photo_path", "{0}") %>
Perhaps I should just hard code the path and just have the image name as the bound bit?
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18791130
Not sure if this is what you want.  I assume you have "c:\inetpub\wwwroot\MyApp\images\photos\2FrontLine.jpg" in your database.
Your site root is  "c:\inetpub\wwwroot\MyApp";
Then you can add a function in your code behind.

    protected string changeUrl(string filePath)
    {
        string root =  Server.MapPath("~");
        root = root.Replace('\\', '/').ToLowerInvariant();
        string p = filePath.Replace('\\','/').ToLowerInvariant();
        if (p.StartsWith(root))
        {
            p = "~" + p.Substring(root.Length);
        }
        return p;
    }

In aspx

<asp:HyperLink Target="_blank" ID="HyperLink1" runat="server" NavigateUrl='<%# changeUrl((string)Eval("photo_path")) %>'
                            Text="Click"></asp:HyperLink>

HTH
0
 
LVL 29

Author Comment

by:QPR
ID: 18797169
the user uploads an image (file). I want to save the filename to the db and have the filename clickable as the hyperlink column.
Up till now I've been trying to insert the entire path into the db and write the entire path (datagrid datasource) to the page. I think I might change the 2nd bit so that only the image name is stored in the db and then on the page I'd like (path to the image folder is constant) to have something like (this is what i would always have done in classic asp....
<a href="/images/photos/"<%=rs(filename)%> & "">click</a>"

I'm not sure how to build the above into a hytextcolumn
0
 
LVL 16

Accepted Solution

by:
Edwin_C earned 2000 total points
ID: 18797831
That is easy.

<asp:HyperLinkField DataNavigateUrlFields="filename" DataNavigateUrlFormatString="/images/photos/{0}"
                    HeaderText="Photo" Text="Click" />

here I assume the data field is named "filename"
0
 
LVL 29

Author Comment

by:QPR
ID: 18803707
HyperLinkField is not a known element.
I changed it to HyperLink and then I get the error that DataNavigateUrlFormatString is not an element of HyperLink.

What you posted looks great (and simple from my end) but is there a syntax error?
0
 
LVL 29

Author Comment

by:QPR
ID: 18804503
Getting there! This is now producing what I need...
 <asp:HyperLinkfield DataNavigateUrlFields="photo_path" DataNavigateUrlFormatString="images/photos/{0}"  HeaderText="Photo" Text="Click" DataTextField="photo_path" DataTextFormatString="Click"></asp:HyperLinkfield>

seems it didn't like it having an ID and didn't like it being inside template tags.
Now all I need to do is work out how to handle those records where there is no data in the filename field.
maybe replace "click" with a "-" and have it NOT clickable.
At present it's pointing to the photos folder (because the first part of the file path is hardcoded)
0
 
LVL 29

Author Comment

by:QPR
ID: 18804509
But I will close this question as you've already provided what I asked and start another. Thanks for all the help and sticking with it.
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18805662
I am happy to help. :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question