Link to home
Start Free TrialLog in
Avatar of JeffEae
JeffEae

asked on

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" />
        <Columns>
          <asp:TemplateField>
           <ItemTemplate>
              <asp:HyperLink  Runat="server" Text='download' NavigateUrl='<%# "Sever.Mappath(".") + Eval("filename")  %>'
                      Target="_self" ID="lnkFile" BackColor="White"></asp:HyperLink>
            </ItemTemplate>
          </asp:TemplateField>
            <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" />
        </Columns>
        <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:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:connstr %>" 
        SelectCommand="SELECT * FROM [UserofFile] WHERE ([FileUser] = @FileUser)" 
            CacheExpirationPolicy="Sliding">
        <SelectParameters>
            <asp:ControlParameter ControlID="userhiddenfield" Name="FileUser" 
                PropertyName="Value" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Open in new window

screenshot002.jpg
Avatar of David Robitaille
David Robitaille
Flag of Canada image

look like you forgot to remove a " 
thry this
NavigateUrl='<%# Sever.Mappath(".") + Eval("filename")  %>'
 
Avatar of JeffEae
JeffEae

ASKER

I've got this now

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

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


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

Avatar of JeffEae

ASKER

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.  

  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.
Avatar of JeffEae

ASKER

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

http://localhost:4870/USCorrugatedtool/'<%#"~"),Eval("filename").ToString();%

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?
Avatar of JeffEae

ASKER

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'?
screenshot003.jpg
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

for your second question: yes.
Avatar of JeffEae

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of David Robitaille
David Robitaille
Flag of Canada image

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
Avatar of JeffEae

ASKER

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