Problem with open a file link

Last post 05-13-2008 9:52 AM by vinhwsu. 3 replies.

Sort Posts:

  • Problem with open a file link

    05-09-2008, 11:56 AM
    • Loading...
    • sivaram123
    • Joined on 02-17-2008, 4:40 PM
    • Posts 42

    HI

    I am having gridview and a hyperlink inside gridview. If I click on the hyperlink, it should open the file (pdf or image or word). the path coming from database. For now, the files stored in local C drive. So how to open the file link inside gridview in new window? The following is th code.

     

    asp:GridView ID="GridViewAttachment" runat="server" AutoGenerateColumns="False" SkinID="ListGridViewStyle" DataSourceID="ObjectDataSource1">

    <Columns>

    <asp:TemplateField HeaderText="Attachments">

    <ItemTemplate>

    <asp:Hyperlink runat="server" ID="Hyperlink1"  Text='<%# Eval("Attachment_FileName") %>'  NavigateUrl ='<%# Bind("Attachment_Path") %>' Target ="_blank"/>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField>

    <ItemTemplate>

    <asp:Image ID= "imagePhotograph" runat="server" Visible='<%# Bind("Attachment_ShowThumbnail") %>' ImageUrl='<%# Bind("Attachment_Path") %>' SkinID="skinAttachment" />

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField>

    </asp:GridView>

    Please help regarding this issue. I worked for several hours on this. But didn't get to work. Its very urgent. Plz help me regarding this.

     

    Thanks in advance...

  • Re: Problem with open a file link

    05-09-2008, 3:10 PM
    • Loading...
    • vinhwsu
    • Joined on 01-11-2007, 1:52 PM
    • Posts 50

    asp:GridView ID="GridViewAttachment" runat="server" AutoGenerateColumns="False" SkinID="ListGridViewStyle" DataSourceID="ObjectDataSource1"  onRowDataBound=gv_RowBound>   

    protected void gv_RowBound(object sender, GridViewRowEventArgs e)
        {
     
                    //System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[0].FindControl("imgPDF");// Controls[0];
                   
                   //this one for button field type image
                   System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[0].Controls[0];//.FindControl("imgPDF");// Controls[0];
       
                  //this one for button field type link
                  //System.Web.UI.WebControls.LinkButton btn = (System.Web.UI.WebControls.LinkButton)e.Row.Cells[8].Controls[0];//.FindControl("imgPDF");//Controls[0];


                    object dataItem = e.Row.DataItem;
                     string Path = DataBinder.Eval(dataItem, "Attachment_Path").ToString();
     

                    if (Path == "")
                    {
                       
                        img.ImageUrl = "Images/noPic.gif";
                        img.Attributes.Add("OnClick", "alert('There is no attached document');return false;");
                    }
                    else
                    {

                        Path = Path.Trim().Replace("\\", "\\\\");

                        img.Attributes.Add("OnClick", "return openPDF('" +_docPath + Path + "');return false;"); //openPDF is a javascript function
                       
                    }
                    e.Row.Cells[0].ToolTip = "Click on the PDF icon to open up the document";
              
                }
                catch (Exception ex) { }
            }
        }


    //Javascript function
    function openPDF(path)
    {
        var opt_windowFeatures = "location=1,status=1,scrollbars=1, resizable=1, width=600, left=670";
        try
        {
          window.open (path, '', opt_windowFeatures);
        }
        catch (err){}
        return false;
    }

  • Re: Problem with open a file link

    05-09-2008, 5:08 PM
    • Loading...
    • sivaram123
    • Joined on 02-17-2008, 4:40 PM
    • Posts 42

    HI,

    Thanks for giving reply. I need to open the file on hyperlink click. The above code I did,'t understand. Please post in vb.net so that its useful for me. Plz help me regarding this

    Thanks

     

  • Re: Problem with open a file link

    05-13-2008, 9:52 AM
    Answer
    • Loading...
    • vinhwsu
    • Joined on 01-11-2007, 1:52 PM
    • Posts 50

    </body>
    <script>
    function openPDF(path)
    {
        var opt_windowFeatures = "location=1,status=1,scrollbars=1, resizable=1, width=600, left=670";
        try
        {
          window.open (path, '', opt_windowFeatures);
        }
        catch (err){}
        return false;
    }
    </script>
    </html>

    -----------------
    asp:GridView ID="GridViewAttachment" runat="server" AutoGenerateColumns="False" SkinID="ListGridViewStyle" DataSourceID="ObjectDataSource1"  onRowDataBound=gv_RowBound>
    -----------------

      Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            Dim link As System.Web.UI.WebControls.HyperLink
            Dim _docPath As String = "path to image"
            For Each ctrl As System.Web.UI.Control In e.Row.Cells(0).Controls
                link = ctrl.FindControl("Hyperlink1")
                If (link.Text <> "") Then

                    Dim dataItem As Object = e.Row.DataItem
                    Dim Path As String = DataBinder.Eval(dataItem, "Attachment_Path").ToString()


                    If (Path = "") Then

                        link.ImageUrl = "Images/noPic.gif"
                        link.Attributes.Add("OnClick", "alert('There is no attached document');return false;") '
                    Else

                        Path = Path.Trim().Replace("\\", "\\\\")

                        link.Attributes.Add("OnClick", "return openPDF('" + _docPath + Path + "');return false;") ' //openPDF is a javascript function

                    End If
                    e.Row.Cells(0).ToolTip = "Click on the PDF icon to open up the document"

                End If
            Next ctrl

        End Sub

     

     

    Try that and see

Page 1 of 1 (4 items)