iframe自动从C#后面的代码调整高度

时间:2013-10-05 07:37:35

标签: c# asp.net

我的html页面是

<iframe runat="server" id="iframe1" width="100%" height="100%" scrolling="no"  frameborder="0"></iframe>
我的pageload事件中的

.cs内容

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

但它不起作用

我想显示整页内容,但我的iframe高度没有达到http://default.com/的高度

3 个答案:

答案 0 :(得分:1)

我不知道如何在.cs页面上自动调整iframe,但它是另一种选择,比如把你的iframe放在datalist控件中......就像...

<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>

将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>

并点击.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();

注意: 这不适用于本地主机..请在线尝试。

答案 1 :(得分:0)

我假设您不想'滚动',那么为什么不禁用它呢?

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

或尝试

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

修改:尝试

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

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

答案 2 :(得分:0)

由于您使用runat="server",因此您可以从后面的代码访问高度和宽度等属性。 尝试

更新答案

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