Link to home
Start Free TrialLog in
Avatar of mlagrange
mlagrangeFlag for United States of America

asked on

DataNavigateUrlFormatString question

Hello - I've got a DataGrid with a data source populated from GetFiles()

I'm trying to use the "FullName" in a HyperLinkField, with the DataNavigateUrlFields = "FullName". Shouldn't I be able to set the DataNavigateUrlFormatString = "{0}", since that has the full path name of the file?

            <asp:HyperLinkField
                DataNavigateUrlFields="FullName"
                DataNavigateUrlFormatString="{0}"
                DataTextField="Name"
                HeaderText="File Name" />

But when I set it uplike this, the file name appears as text, not as a link

What am I doing wrong, please?

Thanks

Mark
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

asp:HyperLinkField has some limitations like you can not freely add multiple field or you have to write extra code for that purpose.  I will recommend using a template column:

<TemplateColumn>
<ItemTemplate>
  <a href='MyPage.aspx?FileName=<%#DataBinder.Eval(Container.DataItem,"FullName")%>&ID=<%#DataBinder.Eval(Container.DataItem,"ID")%>'><%#DataBinder.Eval(Container.DataItem,"Name")%></a>
</ItemTemplate>
</TemplateColumn>

Notice the use of single quotes in a tag.

-Nauman.
Avatar of aki4u
aki4u

Hi,
you have setup parameters in wrong way...

Try using this exaple:

                              
<asp:HyperLinkColumn Text="Click me"
      DataNavigateUrlField="Id"
      DataNavigateUrlFormatString="My_Page.aspx?Id={0}"
      HeaderText="Link column">
</asp:HyperLinkColumn>
Avatar of mlagrange

ASKER

Hi, aki4u - I should provide more details. I am not trying to move to another aspx. I'm displaying a list of pdf files down a particular directory path on the server, down a path like C:\Websites\WebProject\ClientArea\(Client#)\(Year)\(FileName)

I want the clients to be able to click the HyperLink of a given file, so it will prompt them to open or save the that pdf

The GridView gets populated from GetFiles(), so the "FullName" property is there. But when I use "FullName" as the DataNavigateUrlFields value, it does not show up like a link anymore.

As a test, I switched the DataNavigateUrlField to the "Path" property, and hard-coded a path name to a particular client/year in the DataNavigateUrlFormatString, and it works fine IF I start from just the right directory level:

DataNavigateUrlField="Name"
DataNavigateUrlFormatString="\WebProject\ClientArea\000164\2001\{0}"

Is there a way to use a string function in the DataNavigateUrlFormatString to trim off the drive and parent folder name in the FullName ?

Or is there a smarter way?

Thanks

Mark
Why don't you build a full path when you are retriving these files and then just assign it to link control?
mlagrange, what is the root http path of your project? Is it http://localhost/WebProject/ ?

-Nauman.
That's right (actually, it's http://localhost:3200/WebProject/)

Mark
Then DataNavigateUrlFormatString="\WebProject\ClientArea\000164\2001\{0}"  should be

DataNavigateUrlFormatString="WebProject/ClientArea/000164/2001/{0}"

I still recommend you use the TemplateColumn

<TemplateColumn>
<ItemTemplate>
  <a href='WebProject/ClientArea/000164/2001/<%#DataBinder.Eval(Container.DataItem,"FullName")%><%#DataBinder.Eval(Container.DataItem,"FullName")%></a>
</ItemTemplate>
</TemplateColumn>

-Nauman.
Thanks for your response, Nauman, but FullName is the *FULL* name, all the way back to to the C: drive:
C:\Websites\WebProject\ClientArea\000164\2001\(FileName)

and when I've tried that (even hard-coding it), the pathname no longer displays as a link, just text

Maybe I don't understand what the "DataBinder.Eval(Container.DataItem,"FullName")" is doing

I tried to "Edit Templates" on that GridView, and the only options in the template drop-down are "EmptyDataTemplate" and "PagerTemplate". The GridView is bound to the result of a GetFiles() call that occurs when the user clicks a node on an accompanying TreeView control:

    Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged

        Dim directory As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Me.TreeView1.SelectedNode.Value)

        Me.GridView1.DataSource = directory.GetFiles()
        Me.GridView1.DataBind()

    End Sub

The TreeView is populated from a routine in the page OnLoad event that recursed the directories down that path doing DirectoryInfo's

Sorry if this is getting confusing; I will post the code for the page, if you like

Thanks for trying to help

Mark

Why don't you build a full path when you are retriving these files and then just assign it to link control?
E.G.
When you call directory.GetFiles() why not create a column where the link to the file is going to be and just assign that fileld in href?
This is simple and effective solution without "breaking walls" or anything like that.

If you see it difficult, please post a code for GetFiles() function so I can set it up for you.
Sounds good to me! Here's the whoooooole thing:

Partial Class FileDnLoad
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            Dim node As TreeNode = New TreeNode()

            '-- strPlanID is a 6-digit account #, formatted with leading zero's
            node.Value = "C:\Websites\WebProject\ClientArea\PDFs\" & Session("strPlanID") & "\"

            node.Text = "Client " & Session("strPlanID")
            LoadDirectories(node, "C:\Websites\WebProject\ClientArea\PDFs\" & Session("strPlanID") & "\")

            Me.TreeView1.Nodes.Add(node)

        End If

    End Sub
   

    Private Sub LoadDirectories(ByVal parent As TreeNode, ByVal path As String)

        Dim directory As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(path)

        Try
            For Each d As System.IO.DirectoryInfo In directory.GetDirectories()

                Dim node As TreeNode = New TreeNode(d.Name, d.FullName)

                parent.ChildNodes.Add(node)

                'Recurse the current directory
                LoadDirectories(node, d.FullName)
            Next
        Catch ex As System.UnauthorizedAccessException
            parent.Text += " (Access Denied)"
        Catch ex As Exception
            parent.Text += " (Unknown Error: " + ex.Message + ")"
        End Try

    End Sub

    Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged

        Dim directory As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Me.TreeView1.SelectedNode.Value)

        Me.GridView1.DataSource = directory.GetFiles()
       
        Me.GridView1.DataBind()

    End Sub
   
End Class
Please send me the html code of your datagrid.
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="FileDnLoad.aspx.vb" Inherits="FileDnLoad" title="WebProject: File Download" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <h3>File Download</h3>
    <hr />
    <h4>To download a report, select the desired year, then click the link of the desired report. You will then be prompted to "Open" or "Save" the report. </h4>
   <table width="100%" border=0 cellspacing=1>
        <tr></tr>
        <tr>
            <td style="width: 20%" valign="top">
    <asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" Font-Size="Medium">
        <ParentNodeStyle Font-Bold="False" />
        <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
        <SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
            VerticalPadding="0px" ForeColor="#5555DD" />
        <NodeStyle Font-Names="Verdana" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px"
            NodeSpacing="0px" VerticalPadding="0px" />
    </asp:TreeView>
    </td>
    <td style="width: 80%" valign="top">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
        BorderStyle="None" BorderWidth="1px" CellPadding="1" GridLines="Vertical" Font-Size="Small">
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#E0E0E0" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Top" />
        <AlternatingRowStyle BackColor="Gainsboro" />
        <Columns>
            <asp:HyperLinkField
                DataNavigateUrlFields="Name"
                DataNavigateUrlFormatString="\WebProject\ClientArea\PDFs\000164\2003\{0}"
                DataTextField="Name"
                HeaderText="File Name" />
            <asp:BoundField DataField="Length" HeaderText="Size">
                <ItemStyle HorizontalAlign="Right" Width="12%" />
                <HeaderStyle Font-Bold="True" HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="LastWriteTime" HeaderText="Date Modified">
                <HeaderStyle Font-Bold="True" HorizontalAlign="Center" />
                <ItemStyle Width="30%" />
            </asp:BoundField>
        </Columns>        
    </asp:GridView>
            </td>                
        </tr>
    </table>
    <br />
    <h4>You will need the Adobe Reader to view these reports, which can be downloaded for free from
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.adobe.com/products/acrobat/readstep2.html">Adobe's website</asp:HyperLink>.
    </h4>

</asp:Content>

ASKER CERTIFIED SOLUTION
Avatar of aki4u
aki4u

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't forget to replace (Client#)  and (Year())  with data that you are getting from login and dropdownlist.
THANKS!  

But - is this in VB? I'm getting error flags on:

"As FileInfo" - should this be IO.FileInfo?
"As DataTable" - should this be Data.DataTable?

Thanks again, aki4u

Mark
Add this at the top of your page:

Imports System.Data
Imports System.IO
That does it!

Thank you very much, aki4u!

Can I contact you offline? If so, please reply to mlagrange@myway.com

Thanks again

Mark