Sumit
Sumit

Reputation: 277

Open pdf from hyperlink in gridview

want to open pdf file when a user clicks on hyperlink shown in gridview column.The name of the file is taken from first column of the gridview where file titles are stored.

hovering over hyperlink in gridview shows file link, but when clicked it doesnot open pdf file,

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{

 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hlink = (HyperLink)e.Row.FindControl("HL");
            string url = "~/Docs/" + e.Row.Cells[1].Text +".pdf";
            hlink.NavigateUrl = url;
            hlink.Text = "Read";
        } 
 }

The following error comes when hyperlink is clicked, as it is not able to open pdf file.

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable

which event is called when hyperlink is clicked in a gridview, maybe pdf application is to be opened there. Please suggest. Thanks in adavance.

Upvotes: 0

Views: 6740

Answers (2)

Nick Rolando
Nick Rolando

Reputation: 26157

Have you tried checking the "View Source" in the browser on this page to check the actual path of the link that is being outputted and verify it is correct?
Have you stepped through the debugger to make sure that FindControl("HL"); is actually finding the control you are looking for? Since the control is in a GridView, asp.net might have modified its id. You can try getting the HL control using

HyperLink hlink = e.Row.Cells[HL_cell_index].Controls[HL_control_index] as HyperLink;

if you are having issues there. One of those two things must be your problem.

As a suggestion, in agreement with @Jeremy, you should also store the full path of the file in your data source (be it database or whatever you are using) to bind to this hyperlink column, so that you don't have to do this extra event binding and run into issues like this.

Upvotes: 0

Jeremy
Jeremy

Reputation: 9010

The error suggests that your file doesn't exist. What you should do is to forego the concatenation that you do and instead include the whole name of the file (including extension) in your data source and use it (or even the entire path). The reason you want to do it that way is because you may have modified/formatted/encoded the cell text strictly for aesthetic purposes. You could have even added some embedded html to style it.

I would imagine it would look something like this:

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{

 if (e.Row.RowType == DataControlRowType.DataRow)
     {
            DataRow row = ((System.Data.DataRowView)e.Row.DataItem).Row;
            HyperLink hlink = e.Row.FindControl("HL") as HyperLink;
            if (hlink!=null)
            {
                string url = string.Format("~/Docs/{0}",row["FileName"]);
                hlink.NavigateUrl = url;
                hlink.Text = "Read";
            }
     } 
}

Upvotes: 3

Related Questions