Monday, March 12, 2012

How to convert text in GridView col into a hyperlink?

Hi,

One of the fields in a GridView control contains a unc path to a local area network folder [eg:\\servername\foldername].

Each record in the GridView may have a path to a different folder.

I would like to be able to click on the path in the GridView and have the folder open - instead of having to copy the text into the Start | Run dialog box of my pc.

I've looked at converting the column into a hyperlink control [by making it a Templated control] & I've tried this code:

<Columns>
<asp:hyperlinkfield datatextfield="Path"
datanavigateurlformatstring="Path"/>
<asp:BoundField DataField="Path"
HeaderText="Online"
SortExpression="Path" />
</Columns>

. . . and I get an error because the app is trying to open a folder in the directory where the web site resides rather than the folder that is indicated in the path [Text] in the column.

I'm trying to figure out how to tell asp.net to open the folder indicated by the unc path in the field . . .Confused

I'd appreciate any help I can get.

Thanks!

Robin

What most likely is happening, is all links in a website by default are relative, so you end up with a link to something like this

http://localhost:12345:\\servername\path

Which obviously isnt going to work. You can fix it by prefixing it with something it recognizes as a file path, rather than a relative path.


Try replacing your <asp:HyperLink /> with this:

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:HyperLinkText="TextField"id="myHL"runat="server"

NavigateUrl='<%# "file:///" + DataBinder.Eval(Container.DataItem, "Path").ToString() %>'></asp:HyperLink>

</ItemTemplate>

</asp:TemplateField>

</Columns>

Thanks, Hans! That worked.

I have one more question, though. In the Text = "TextField" - I would like the text that displays be the actual path that is coming in from the database.

So - instead of TextField [hard-coded literal] in the GridView I'd like it to have something like this -\\ServerName\FolderName [what is in the field for each record].

How would I do that?

Robin


Text='<%# Databinder.Eval(Container.DataITem, "Path").tostring() %>'


Thanks, Bryan. Perfect! I tried playing around with that line of code from Hans . . .

Robin

Labels: , , , , , , , , , , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home