Solved

Download link

Posted on 2013-01-30
5
252 Views
Last Modified: 2013-02-17
Hi,

The following code displays a download link in a datagridView control and it's working. How can modify the code to remove the hyperlink if no file is available?

 protected void grdProjects_RowCommand (object sender, GridViewCommandEventArgs e)
   {
       string name = e.CommandArgument.ToString ();
       string fullFilePath = Server.MapPath (name);
          System.IO.FileInfo fileInfo = new System.IO.FileInfo (fullFilePath);
          this.Response.Clear ();
          this.Response.AddHeader ("Content-Disposition", "attachment; filename=" + fileInfo.Name);
          this.Response.AddHeader ("Content-Length", fileInfo.Length.ToString ());
          this.Response.ContentType = "application/octet-stream";
          //this.Response.TransmitFile(fileInfo.FullName);
          this.Response.BinaryWrite (File.ReadAllBytes (fileInfo.FullName));
          this.Response.End ();
   }

html
       <Columns>
          <asp:TemplateField HeaderText="Misc-Doc">
              <ItemTemplate>
                  <asp:LinkButton ID="btnItemName6" runat="server" Text='View' CommandArgument='<%# Eval("md") %>'
                     Font-Underline="false" ForeColor="black" CssClass="linkbutton" CommandName="OpenFile" />
              </ItemTemplate>
          </asp:TemplateField>

Please help.

ayha
0
Comment
Question by:ayha1999
5 Comments
 
LVL 18

Expert Comment

by:dj_alik
ID: 38835376
protected void grdProjects_RowCommand (object sender, GridViewCommandEventArgs e)
   {
...................... 

if(!File.Exists(fullFilePath ))
{
int rowIndex = ((e.CommandSource as LinkButton).NamingContainer as GridViewRow).RowIndex;
                for (int i = 0; i < grdProjects.Rows.Count; i++)
                {
                        if (i != rowIndex)
                                (grdProjects.Rows[i].FindControl("LinkButtonStop") as LinkButton).Enabled = false;
                }
}
}

Open in new window

0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 38835385
Try this:

       string name = e.CommandArgument.ToString ();
       string fullFilePath = Server.MapPath (name);
        if(System.IO.File.Exists(fullFilePath)) {
              System.IO.FileInfo fileInfo = new System.IO.FileInfo (fullFilePath);
              this.Response.Clear ();
              this.Response.AddHeader ("Content-Disposition", "attachment; filename=" + fileInfo.Name);
              this.Response.AddHeader ("Content-Length", fileInfo.Length.ToString ());
              this.Response.ContentType = "application/octet-stream";
              //this.Response.TransmitFile(fileInfo.FullName);
              this.Response.BinaryWrite (File.ReadAllBytes (fileInfo.FullName));
              this.Response.End ();
        }

Hope that helps.
0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 38835572
Hi,

Try adding the visible property for the link button like this:

Visible='<%# Eval("<file_Filed_from_DB>").ToString() != "" %>' if you return an empty string from the query when there is no file present to download.


Regards,
Mishu
0
 
LVL 10

Expert Comment

by:Monica P
ID: 38842888
code to remove the hyperlink if no file is available

grdProjects_RowCommand


before assigning the filelocation

check the file exist in the current location  ,if exits assign hyperlink else
use FINDCONTROL and make that hyperlink control readyonly or visible as none

If File.Exists("C:\MyDocs\MyFile.txt") = False Then
   MsgBox("Field does not exist!", MsgBoxStyle.Critical, "File Not Found")
   Application.Exit()
End If

Open in new window


http://www.brangle.com/wordpress/2009/08/check-if-file-exists-wit-vb-net/
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 38898725
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question