Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hyperlink Datagrid 1

Status
Not open for further replies.

scottydd

IS-IT--Management
Sep 19, 2002
31
0
0
US
I would like to know if the following is possible using C#:

I am querying a access database for 4 fields. I then use the first 3 fields in a datagrid. What I would like to do is use the 4th field (which is the location of a Word Document on my server) as the hyperlink for field 1, and have the hyperlink open the Word docuent. Can anyone help?

Thanks
Scott
 
set up the 4th field as a Link field and bind it...see what happens...

dlc
 
If the 4th field is the physical location of the document (i.e. D:\Files\foo.doc), then you'll have to write some kind of handler to turn that into a virtual path (i.e.
One method of doing this (please excuse any syntax errors, this was all written freehand) might be:

Set up your datagrid like this:
Code:
<asp:DataGrid runat=server>
<Columns>
    <asp:TemplateColumn>
        <ItemTemplate>
            <a target="_new" href='<%# this.GetDocUrl(DataBinder.Eval(Container,"DataItem.[FileUrlColumn]")) %>'>[DataBound Title]</a>
        </ItemTemplate>
    </asp:TempateColumn>
</Columns>
</asp:DataGrid>

Add a key like this one in you web.config settings (remember to set up the matching virtual directory in iis):
Code:
<appSettings>
    <add key="FileVRoot" value="[URL unfurl="true"]http://localhost/files/"/>[/URL]
</appSettings>

And then add this to your datagrid's page code. This will change the physical url stored in your database to the virtual directory set up in iis and your web.config file:
Code:
protected string GetDocUrl(string docPhysUrl)
{
    /* returns the virtual path of the file with the physical file name appended */
    System.Configuration.ConfigurationSettings.AppSettings["FileVRoot"] + docPhysUrl.Substring(docPhysUrl.LastIndexOf(@"\"));
}

Another method, which I would highly recommend, would be to store the files as Image types in SQL and output them to the client on the fly instead of dealing with any file IO stuff (but that's another post ;))

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
I figured it out i just used some old code from my asp page.
Code:
<asp:TemplateColumn>
  <ItemTemplate>
    <table>
      <tr>
        <td>
          <a href="file:<%#DataBinder.Eval(Container.DataItem, "server_location" )%>" target="_blank">Open File</a></td>
      </tr>
    </table>
  </ItemTemplate>
</asp:TemplateColumn>
Thanks for the help.
 
If you're publishing this to the web, you'll want to make sure that it is available to external clients. If you're running it at localhost and your stored path is D:\Files\foo.doc, it won't work for anybody else visiting your site.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Atomic - good post, nice contribution to the FAQ on DataGrid variables (FAQ faq855-4372) - will add this link.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top