Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Download link

Posted on 2013-01-30
5
Medium Priority
?
258 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

916 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