[英]How to set the page size while opening the window?
我正在使用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>");
腳本內的寬度僅設置寡婦的寬度。 但是我想設置頁面的寬度。
可能嗎?
謝謝..
編輯:
抱歉,我之前沒有提到它。我正在將內容動態寫入print.aspx中,我正在使用此代碼填充頁面。
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頁面。 因此,我希望此內容應處於靜態頁面寬度。
在window.open()函數內部指定的寬度將僅設置彈出窗口的寬度。 如果要更改頁面的寬度,則應嘗試更改Print.aspx中的寬度。
您不能使用window.open
更改頁面的寬度,它僅設置窗口屬性。
若要更改頁面的width
,可能需要更改包裝的div
元素或頁面body
的寬度,可能需要更改print.aspx的樣式。
var newwindow = window.open('Print.aspx');
var elem = newwindow.document.getElementById('my-id');
elem.style.width = 'XYZpx'
PS:-以上代碼僅在新窗口具有相同的協議,域和端口的情況下起作用。 如果它在另一個域上,出於安全原因,您不能執行此操作。
如果僅使用window.open方法使用該頁面,則分別設置硬編碼的頁面寬度,否則,在使用window.open打開頁面時注入尺寸。
打開時如何注射:
使用Javascript從父窗口訪問子窗口元素 (注意跨域資源共享的安全性問題)
簡而言之:
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 = ; //
如何將其設置為硬編碼:
您要更改窗口中哪個元素的寬度? body
? div
?
我猜您正在計算頁面body
左右兩側的margin
。
嘗試在您的head
標簽中插入以下樣式塊:
<style>
body{margin: 0 10px 0 10px;}
</style>
其中10px
是左右邊距。
您也可以使用
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.