HyperLinkField not working

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)
LVL 29
QPRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Edwin_CCommented:

 <asp:HyperLinkField DataNavigateUrlFields="photo_path" HeaderText="Photo" Text="Click" Target="_blank"  />
0
QPRAuthor Commented:
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
QPRAuthor Commented:
an example of the fields value is....
c:\inetpub\wwwroot\myApp\images\photos\Footpath.gif
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Edwin_CCommented:
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
QPRAuthor Commented:
Nope same result.
It's worked for me in the past, I'm stumped as to what is going on here
0
Edwin_CCommented:
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
QPRAuthor Commented:
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
Edwin_CCommented:
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
QPRAuthor Commented:
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
QPRAuthor Commented:
' 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
Edwin_CCommented:
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
QPRAuthor Commented:
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
Edwin_CCommented:
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
QPRAuthor Commented:
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
Edwin_CCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QPRAuthor Commented:
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
QPRAuthor Commented:
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
QPRAuthor Commented:
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
Edwin_CCommented:
I am happy to help. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.