2012-10-03 50 views
0

我使用的是ASP.NET和C#,我使用window.open()打开窗口。现在我需要在该窗口中设置该页面的宽度。如何在打开窗口时设置页面大小?

我在buttonclick事件上使用此代码。

ClientScript.RegisterStartupScript(this.GetType(), "oncl", "<script language=javascript>window.open('Print.aspx','PrintMe','width=900,resizable=1,scrollbars=1');</script>"); 

脚本内部的宽度仅设置窗口宽度。但我想设置页面的宽度。

可能吗?

谢谢..

编辑:

对不起,我没有提到它before.I我写的内容转换成print.aspx dynamically.That我使用这个代码填充页面。

Page pg = new Page(); 
    pg.EnableEventValidation = false; 
    HtmlForm frm = new HtmlForm(); 
    pg.Controls.Add(frm); 
    frm.Controls.Add(ctrl); 
    pg.DesignerInitialize(); 
    pg.RenderControl(htmlWrite); 
    string strHTML = stringWrite.ToString(); 
    HttpContext.Current.Response.Clear(); 
    HttpContext.Current.Response.Write(strHTML); 

这在pageload事件上被调用。所以我打印print.aspx中的.aspx页面。所以我想这个内容应该在静态页面宽度。

+0

它并不真正的问题,你可以使用JS从外面改变它,就像我在答案中解释的那样。 –

回答

1

在window.open()函数内指定的宽度将只设置弹出窗口的宽度。如果你想改变页面的宽度,那么你应该尝试改变Print.aspx中的宽度。

1

您不能使用window.open更改页面的宽度,它只设置窗口属性。

改变页面的width你需要改变的print.aspx 可能的风格,如果你要改变包装div元素的页

var newwindow = window.open('Print.aspx'); 
var elem = newwindow.document.getElementById('my-id'); 
elem.style.width = 'XYZpx' 

P.S.的宽度或body : - 上述代码仅适用于新窗口具有相同协议,域和端口的情况。如果它位于另一个域中,则出于安全原因无法执行此操作。

1

如果您仅使用window.open方法使用该页面,则分别设置页面的宽度为硬编码,否则在使用window.open打开页面时注入大小。

如何注入,同时开口道:

Access child window elements from parent window using Javascript (注意对Cross-origin resource sharing安全问题)

简而言之:

var childWindow = window.open('Print.aspx','PrintMe','width=900,resizable=1,scrollbars=1'); 
childWindow.document.getElementById('someDivId').style.width = '400px'; 

,或者在服务器端访问它:

HtmlForm frm = new HtmlForm(); 
frm.style = ; // 

read here

如何设置硬编码:

宽度元素在窗口中的你想改变什么? bodydiv

我想你是在拍下有关margin左&你的页面的权利body

尝试插入下面的样式块到你head标签:

<style> 
body{margin: 0 10px 0 10px;} 
</style> 

其中10px是左,右页边距。

1

您还可以使用

window.resizeTo(x,y); 

比如:

function resizeWindow() 
{   
    // you can get height and width from serverside as well  
    var width=400; 
    var height=400; 
    window.resizeTo(width,height);   
} 

上的onload()体的情况下调用该函数

<body onload="resizeWindow()" > 
    body of page 
</body>