Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 637
  • Last Modified:

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
0
mlagrange
Asked:
mlagrange
  • 7
  • 7
  • 3
1 Solution
 
nauman_ahmedCommented:
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.
0
 
aki4uCommented:
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>
0
 
mlagrangeAuthor Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

Mark
0
 
nauman_ahmedCommented:
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.
0
 
mlagrangeAuthor Commented:
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

0
 
aki4uCommented:
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.
0
 
mlagrangeAuthor Commented:
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
0
 
aki4uCommented:
Please send me the html code of your datagrid.
0
 
mlagrangeAuthor Commented:
<%@ 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>

0
 
aki4uCommented:
OK.

Here is your function that will put everything to datatable:

    Private Function GetFilesDataTable(ByVal arrFi As FileInfo()) As DataTable
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("Name")
        dt.Columns.Add("Length")
        dt.Columns.Add("LastWriteTime")
        dt.Columns.Add("PathToTheFile")

        'For Each fi As FileInfo In arrFi
        Dim cnt As Integer = 0
        For cnt = 0 To arrFi.Length - 1
            dt.Rows.Add(dt.NewRow())
            dt.Rows(cnt)(0) = arrFi(cnt).Name
            dt.Rows(cnt)(1) = arrFi(cnt).Length
            dt.Rows(cnt)(2) = arrFi(cnt).LastWriteTime
            dt.Rows(cnt)(3) = "\WebProject\ClientArea\" & (Client#) & "\" & (Year()) & "\" & arrFi(cnt).Name
        Next
        Return dt
    End Function


Call it like this (replace this line in your code):
Me.GridView1.DataSource = GetFilesDataTable(directory.GetFiles())



Now, in your html view, replace your HyperLinkField with this code:
            <asp:HyperLinkField
                DataNavigateUrlFields="PathToTheFile"
                DataNavigateUrlFormatString="{0}"
                DataTextField="Name"
                HeaderText="File Name" />


That's it...compile it run it.
0
 
aki4uCommented:
Don't forget to replace (Client#)  and (Year())  with data that you are getting from login and dropdownlist.
0
 
mlagrangeAuthor Commented:
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
0
 
aki4uCommented:
Add this at the top of your page:

Imports System.Data
Imports System.IO
0
 
mlagrangeAuthor Commented:
That does it!

Thank you very much, aki4u!

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

Thanks again

Mark
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now