• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 963
  • Last Modified:

Setting up a hyperlink inside a gridview

I've got a gridview and need to finish setting up a download function.  I've got a hyperlink attached but need help with the NavigateUrl proerty.  The names of the files are represented on the gridview on the picture attached, in the column called 'File Name'.  The files themselves I just uploaded using Server.MapPath(".\\").  So I need some help actually being able to call the server and then add the filename from that column.  I took a wild guess in the markup below, just to give you an idea of what I'm stuck on.  Thanks
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" 
        GridLines="None" style="margin-right: 0px" Width = "1000px">
        <RowStyle BackColor="#EFF3FB" />
              <asp:HyperLink  Runat="server" Text='download' NavigateUrl='<%# "Sever.Mappath(".") + Eval("filename")  %>'
                      Target="_self" ID="lnkFile" BackColor="White"></asp:HyperLink>
            <asp:BoundField DataField="fileid" HeaderText="ID" 
                SortExpression="fileid" />
            <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
            <asp:BoundField DataField="subtitle" HeaderText="Subtitle" 
                SortExpression="subtitle" />
            <asp:BoundField DataField="nameoffile" HeaderText="File Name" 
                SortExpression="nameoffile" />
            <asp:BoundField DataField="date" HeaderText="Date" SortExpression="Date" />
            <asp:BoundField DataField="category" HeaderText="Category" 
                SortExpression="category" />
            <asp:BoundField DataField="FileUser" HeaderText="User" 
                SortExpression="FileUser" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" BorderColor="#0033CC" Font-Bold="True" 
            ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:connstr %>" 
        SelectCommand="SELECT * FROM [UserofFile] WHERE ([FileUser] = @FileUser)" 
            <asp:ControlParameter ControlID="userhiddenfield" Name="FileUser" 
                PropertyName="Value" Type="String" />

Open in new window

  • 6
  • 6
1 Solution
David RobitailleAnalyst ProgrammerCommented:
look like you forgot to remove a " 
thry this
NavigateUrl='<%# Sever.Mappath(".") + Eval("filename")  %>'
JeffEaeAuthor Commented:
I've got this now

              <asp:HyperLink Runat="server" Text='download' NavigateUrl='<%#Server.Mappath(".") + Eval("filename")%>'</asp:HyperLink>

That's given me two errors:
Error      1      The server tag is not well formed.      
Warning      2      Validation (ASP.Net): Element 'HyperLink' is missing the '>' character from its start tag.      

I wasn't really posting that as the answer, because I don't know if Eval("filename") is actually going to return the filename listed on the row in the pic.  That's what I need help on

David RobitailleAnalyst ProgrammerCommented:
ok, i copy paste something from my code that i know it work.
            <asp:HyperLink ID="TemplateOptionOrderTop" runat="server" 
                           NavigateUrl='<%# string.format("./TemplateOptionOrder.aspx?TemplateID={0}",eval("Template_ID").tostring()) %>'
                           Text="Edit options Order" />

Open in new window

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

JeffEaeAuthor Commented:
Yea, but that's not applicable to my two problems, I don't really know what you want me to do with that

1) How to call the server in the NavigateUrl field (guessing its sever.mappath but I need some verification)

2) How to call the corresponding row in the table named filename (really really guessing that its something like Eval("filename") even though I don't know if that's going to do it.  

David RobitailleAnalyst ProgrammerCommented:
  1. Navigate url are just url. they are rendered as a href in the resulting page.  sever.mappath will return the url in the server s point of view (c:\inetpub...) and it<s not what you need. you need to send a link to the file so the clinet will download the file from this link. Sso you don`t need to "resolve" the path.  Also since HyperLink are server control, you could also use the tilde (~) to set the path has the root of your website.
  2. Eval("filename")  will get the GridView datasource and return the value of the "filename" field. i think we are saying the same thing so you should be ok with that part.
JeffEaeAuthor Commented:
So cool, I don't need Server.Mappath.  

So you're saying since all the files are stored on the root of the website, I can just use the tilde?

NavigateUrl='<%#"~")+ Eval("filename").ToString();%>'

I ran that and here's what came up


And of course that's an HTTP: bad request error

I appreciate your patience and willingness to help

On #2, will it get the current row though, that's the real question.  By current row, I mean the row where the hyperlink that the user is clicking on.  Instead of giving me Eval(filename) will it give me whatever is in that row?
JeffEaeAuthor Commented:
Here's a screenshot to illustrate my second question

You can see the column 'File Name' has an entry, 'feedreader.exe'.  By using Eval("filename"), will that return 'feedreader.exe'?
David RobitailleAnalyst ProgrammerCommented:
noticed i use string.format. it replace parameter using {0}, {1} ... I suggest you stat use those.
 in you case it should be :

NavigateUrl='<%# String.Format("~/{0}", Eval("filename").ToString()); %>'

Open in new window

David RobitailleAnalyst ProgrammerCommented:
for your second question: yes.
JeffEaeAuthor Commented:
OK almost there.

I copied that and got 'server tag is not well formed'
  <asp:HyperLink ID="HyperLink1" Runat="server" Text="Download" NavigateUrl='<%#String.Format("~/{0}", Eval("filename").ToString()); %>'</asp:HyperLink>

Open in new window

David RobitailleAnalyst ProgrammerCommented:
here you go
<asp:HyperLink ID="HyperLink1" Runat="server" Text="Download" NavigateUrl='<%#String.Format("~/{0}", Eval("filename").ToString()); %>' ></asp:HyperLink>

Open in new window

JeffEaeAuthor Commented:
Thank you so much! My boss was gonna kill me! lol

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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