2012-10-03 51 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>