Search This Blog

Sunday, August 6, 2017

Resize Iframe Dynamically in C#

<iframe runat="server" width="100%" id="IFReport" src="" frameborder="0"clientidmode="Static" height="500"></iframe>

Write a JavaScript method for resizing it, as in:

<script type="text/javascript">    function setIframeHeight(iframe) {        if (iframe) {            var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;            if (iframeWin.document.body) {                iframe.height = 17 + iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;            }        }    };</script>
As you notice, I have set the runat="Server" of the iframe. So we can't call the setIframeHeight method directly by writing the onload of the iframe.

We will set the iframe attribute for calling that method as in:
this.IFReport.Attributes.Add("onload""setIframeHeight(document.getElementById('" + IFReport.ClientID + "'));");

No comments:

Post a Comment