Resize Iframe Dynamically in C#

In this article I’ll explain how we can resize an iframe programmatically.

In some scenarios we want to hide the vertical scroll of an iframe. We can do that either by using JavaScript or jQuery.

Let's drag an IFrame from the toolbox.

<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 + "'));");

By this way we can resize the iframe.