Solved

Download link

Posted on 2013-01-30
5
251 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now