user2256404
user2256404

Reputation: 115

display aspx in iframe and aspx is displaying pdf

I using an iframe to display an aspx page as follows

<a href="#" id="trigger">this link</a>
<div id="dialog" style="display:none">
    <div>
    <iframe frameborder="0"  height="600" width="600" src="Displaypdf.aspx"></iframe>
 </div>
    </div>

In the Displaypdf.aspx.cs, I am displaying a pdf as follows. I have a button in the aspx, and on clicking the button the pdf is displayed (in the aspx page which is now an iframe)

protected void Button1_Click(object sender, EventArgs e)
{
    string FilePath = Server.MapPath("sample.pdf");
    WebClient User = new WebClient();
    Byte[] FileBuffer = User.DownloadData(FilePath);
    if (FileBuffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", FileBuffer.Length.ToString());
        Response.BinaryWrite(FileBuffer);
    }
}

this works fine. Now I want to do the same thing at Page_Load But when i put this code in page load, this does not work.

protected void Page_LOad(object sender, EventArgs e)
{
    string FilePath = Server.MapPath("sample.pdf");
    WebClient User = new WebClient();
    Byte[] FileBuffer = User.DownloadData(FilePath);
    if (FileBuffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", FileBuffer.Length.ToString());
        Response.BinaryWrite(FileBuffer);
    }
}

What happens is, I click and get my iframe, but iframe is blank, because first time mein pdf is not getting rendered. can someone tell me how to fix this problem.

Upvotes: 2

Views: 5355

Answers (1)

connersz
connersz

Reputation: 1213

I have tried your code and I think you are missing a couple of things to make it work. I have provided an edited version of your code below which should work OK.

The iframe page (aspx):

<head>
    <script src="../../Scripts/jquery-1.9.1.min.js"></script>
    <script src="../../Scripts/jquery-ui.js"></script>

   <script type="text/javascript">
     var $dial1 = ""

function openlink(url, title, width, height) {
    $dial1 = $('<div></div>')
                   .html('<iframe id="frame1" style="border: 0px; " src="' + url + '" width="100%" height="100%"></iframe>')
                   .dialog({
                       autoOpen: false,
                       modal: true,
                       height: height,
                       width: width,
                       title: title
                   });
    $dial1.dialog('open');

</script>


</head>


    <body>
        <form id="form1" runat="server">
            <asp:Button ID="Button1" runat="server" Text="This Link" OnClientClick="openlink('Displaypdf.aspx', 'Open', '1000', '470'); return true;" OnClick="Button1_Click" />


        </form>
    </body>

Displaypdf.aspx.cs:

   protected void Page_Load(object sender, EventArgs e)
        {
            string FilePath = Server.MapPath("sample.pdf");
            WebClient User = new WebClient();
            Byte[] FileBuffer = User.DownloadData(FilePath);
            if (FileBuffer != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", FileBuffer.Length.ToString());
                Response.BinaryWrite(FileBuffer);
            }
        }

Hope this helps...

Upvotes: 1

Related Questions