Solved

Download link

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Round up to 100% in .NET 10 45
VB.NET HttpWebRequest 12 32
Calling stored proc in EDMX 11 19
Unable  to create new object 9 25
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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

22 Experts available now in Live!

Get 1:1 Help Now