imagebutton opens a new window but with onclick

Last post 05-15-2008 11:56 AM by DaveC426913. 11 replies.

Sort Posts:

  • imagebutton opens a new window but with onclick

    05-14-2008, 4:12 PM

    This is what I'm starting with:

    <asp:ImageButton ID="tdsBtn" runat="server" ImageUrl="images/design/but_download_PDF.gif" OnClick="tdsBtn_Click" /> 

    string tdsPDF = getPDFfilename(); 
    
    protected void tdsBtn_Click(object sender, ImageClickEventArgs e)
    {
        string pdfLink = "pdfs/tds/" + tdsPDF;
        Response.Redirect(pdfLink, true);
    }
    
    
    Now I want the link to open in a new window.
    There's no target= attribute for the ImageButton control. I could replace the ImageButton with a plain ol  <a><img /></a>  but then I'll have to add the event and write the JavaScript?
    Ideas?
  • Re: imagebutton opens a new window but with onclick

    05-14-2008, 5:00 PM
    • Loading...
    • klin6on
    • Joined on 04-07-2008, 10:49 PM
    • Posts 94

     Hi,

     you can use <a><img /></a> and set the href property to a .aspx page dedicated to display pdf or docs in general. Use query string to pass the file name or other parameter to the dedicated page and in the Load event of this page, run your code that redirects to the file.

    Example: <a href='DisplayPDF.aspx?filename=file1'><img /></a>

    Page_Load(object sender, eventargs e)

    {

        string fileName = Request.QueryString["filename"].ToString();

        ...

    }
     

    klin6on
  • Re: imagebutton opens a new window but with onclick

    05-14-2008, 5:02 PM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 3:47 PM
    • Cebu Philippines
    • Posts 6,025

    You can use the JavaScript showModalDialog or window.Open method to open a new window .. see below 

    <script type="text/javascript" language="javascript">
    function Show()

    {

      window.showModalDialog("Nextpage.aspx",'', '');


    }
    </script>

    <asp:ImageButton ID="tdsBtn" runat="server" ImageUrl="images/design/but_download_PDF.gif" OnClick="tdsBtn_Click" OnClientClick="show();" />  

    Cheers,
    Vincent Maverick Durano


  • Re: imagebutton opens a new window but with onclick

    05-14-2008, 5:13 PM

    " you can use <a><img /></a> and set the href property to a .aspx page dedicated to display pdf or docs in general. Use query string to pass the file name or other parameter to the dedicated page and in the Load event of this page, run your code that redirects to the file. "

    Is this really necessary? Can't I just open the PDF in a new window?

  • Re: imagebutton opens a new window but with onclick

    05-14-2008, 5:25 PM

    "...You can use the JavaScript showModalDialog or window.Open method to open a new window..." 

    You do realize that the path to the file is generated in the codebehind. If it weren't I'd just use   

    <a href="#" onclick="winodw.open(...)">

     

  • Re: imagebutton opens a new window but with onclick

    05-14-2008, 5:31 PM

    what gives with these quotes?

  • Re: imagebutton opens a new window but with onclick

    05-14-2008, 5:34 PM
    • Loading...
    • klin6on
    • Joined on 04-07-2008, 10:49 PM
    • Posts 94

     Hi,

    can you generate the path when you load your page and set it explicitly to the <a> tag. I am not sure what is the reason to postback and get the path on postback? 

    klin6on
  • Re: imagebutton opens a new window but with onclick

    05-14-2008, 5:40 PM
    • Loading...
    • klin6on
    • Joined on 04-07-2008, 10:49 PM
    • Posts 94

    Can you use this?

    <a href="<%# GetFilePath() %>" target="blank"><img src=/></a> 

    where GetFilePath() is a function in your code-behind? 

    klin6on
  • Re: imagebutton opens a new window but with onclick

    05-15-2008, 9:49 AM

    klin6on:

    Can you use this?

    <a href="<%# GetFilePath() %>" target="blank"><img src=/></a> 

    where GetFilePath() is a function in your code-behind? 

    Yeah but I'm still doing something wrong. My method returns nothing.

     

     
    <a href="<%# getFile() %>" target="_blank">
     
     
    protected string getFile()
    {
        return "pdfs/msds/MSDS_P0DS01.PDF";
    }
    
     
    Gets me:
     
     
    <a href="" target="_blank">
  • Re: imagebutton opens a new window but with onclick

    05-15-2008, 10:44 AM
    • Loading...
    • klin6on
    • Joined on 04-07-2008, 10:49 PM
    • Posts 94

    Hi,

    add

    this.DataBind();

    to the page_load event handler. 

    klin6on
  • Re: imagebutton opens a new window but with onclick

    05-15-2008, 11:40 AM

     

    Thank you. That works. I'm 3/4ths of the way there. Only problem remaining is that the PDF does not open in the new window. It chokes.

     That same file will open in the same page, but not in a new window - it just hangs.

     Is this something to do with configuring the mime-type?

  • Re: imagebutton opens a new window but with onclick

    05-15-2008, 11:56 AM
    Answer

    Never mind. That was a separate issue - incomplete Acrobat update.

     

    So, the answer is:

     

    <a href="<%# getFilePath() %>" target="_blank">  
    
    
    protected string getFilePath()
    {
        return "pdfs/msds/" + getPDFileName();";
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
            this.DataBind();
    }
     
Page 1 of 1 (12 items)