Solved

Download link

Posted on 2013-01-30
5
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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