简体   繁体   English

iframe从C#中的代码隐藏自动调整高度

[英]Iframe autoresizing height from codebehind in C#

My html page is 我的html页面是

<iframe runat="server" id="iframe1" width="100%" height="100%" scrolling="no"  frameborder="0"></iframe>

.cs content in my pageload event 我的页面加载事件中的.cs内容

iframe1.Attributes["src"] = "http://default.com/";
//iframe1.Attributes["height"] = "100%";
//iframe1.Attributes["width"] = "100%";
iframe1.Attributes.Add("style","width:100%;height:100%;");

But its not working 但是它不起作用

i want to display whole page content but my height of iframe is not taking the height of http://default.com/ 我想显示整个页面的内容,但我的iframe高度不采用http://default.com/的高度

I don't know how to autoresize iframe on .cs page but It's another option like put your iframe in datalist control like... 我不知道如何在.cs页上自动调整iframe的大小,但这是另一种选择,例如将iframe放在数据列表控件中,例如...

<asp:DataList ID="dtlhtml" runat="server" Width="100%">
    <ItemTemplate>
        <table cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td>
                    <iframe src='<%#Eval("html") %>' width="713" id="iframe1" 
                        frameborder="0" onLoad="autoResize 'iframe1');">
                    </iframe>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

Put javascript code as... 将javascript代码作为...

<script language="JavaScript">
function autoResize(id) 
{
    var newheight;
    var newwidth;
    if (document.getElementById(id))
    {
        newheight = document.getElementById(id).contentWindow.document.body.scrollHeight;
        newwidth = document.getElementById(id).contentWindow.document.body.scrollWidth;
    }
    document.getElementById(id).height = (newheight) + "px";
    document.getElementById(id).width = (newwidth) + "px";
}
</script>

And put on .cs page. 并放在.cs页面上。

DataTable dt1 = new DataTable();
dt1.Columns.Add("html");
DataRow dr = dt1.NewRow();
dr["html"] = "";//Any dynamic url path
dt1.Rows.Add(dr);
dtlhtml.DataSource = dt1;
dtlhtml.DataBind();

NOTE: This will not work in local host ..please try it on online. 注意: 这在本地主机中不起作用。请在线尝试。

I assume you don't want 'scrolling', so why not disable it? 我假设您不想“滚动”,那么为什么不禁用它呢?

<iframe src="/default.asp" width="100%" height="100%" scrolling="no"></iframe>

or try 或尝试

iframe1.Attributes.Add("scrolling","no");

Edit: Try 编辑:尝试

PlaceHolder1.Controls.Add(new LiteralControl("<iframe src='mypage.aspx' width='100%' height='100%' scrolling='no'></iframe>"));

or 要么

iframe1.Attributes["src"] = "http://www.asp.net";

Since you are using runat="server" so you can access the attributes like height and width from code behind. 由于您使用的是runat="server"因此您可以从后面的代码访问高度和宽度之类的属性。 Try 尝试

Updated Answer 更新的答案

iFrame1.Attributes.Add("height","100%");
iFrame1.Attributes.Add("width","100%");
set scrolling ="no" inside tag as suggested by Paul

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM