Solved

HyperLinkField not working

Posted on 2007-03-21
19
970 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 500 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now