简体   繁体   English

打开窗口时如何设置页面大小?

[英]How to set the page size while opening the window?

I am using ASP.NET and C# and I am using window.open() for opening the window.Now i need to set the width of that page in that window. 我正在使用ASP.NET和C#,并且正在使用window.open()打开窗口。现在我需要在该窗口中设置该页面的宽度。

I am using this code on buttonclick event. 我在buttonclick事件上使用此代码。

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

The width inside the script is only setting the widow width. 脚本内的宽度仅设置寡妇的宽度。 But i want to set the width of the page. 但是我想设置页面的宽度。

Is it possible? 可能吗?

Thanks.. 谢谢..

Edit: 编辑:

Sorry I didn't mention it before.I am writing the content into print.aspx dynamically.That i am using this code for fill the page. 抱歉,我之前没有提到它。我正在将内容动态写入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);

This is called on pageload event. 这是在pageload事件中调用的。 So i am printing the .aspx page in print.aspx. 所以我在print.aspx中打印.aspx页面。 So i want this content should be in the static page width. 因此,我希望此内容应处于静态页面宽度。

Width specified inside window.open() function will only set width of the popup window. 在window.open()函数内部指定的宽度将仅设置弹出窗口的宽度。 If you want to change the width of the page then you should try changing the width inside Print.aspx. 如果要更改页面的宽度,则应尝试更改Print.aspx中的宽度。

You cannot change width of the page using window.open , it only sets the window properties. 您不能使用window.open更改页面的宽度,它仅设置窗口属性。

To change the width of the page you need to change the style of print.aspx probably if you have to change the width of the wrapped div element or body of the page 若要更改页面的width ,可能需要更改包装的div元素或页面body的宽度,可能需要更改print.aspx的样式。

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

PS :- The above code will only work if the new window has the same protocol, domain and port. PS:-以上代码仅在新窗口具有相同的协议,域和端口的情况下起作用。 If it's on another domain, you can't do this for security reasons. 如果它在另一个域上,出于安全原因,您不能执行此操作。

In case which you're using that page only with the window.open method then set the width of the page respectively hard-coded, otherwise, inject the size while opening the page using window.open. 如果仅使用window.open方法使用该页面,则分别设置硬编码的页面宽度,否则,在使用window.open打开页面时注入尺寸。

How to inject while opening: 打开时如何注射:

Access child window elements from parent window using Javascript (Notice to security issues on Cross-origin resource sharing ) 使用Javascript从父窗口访问子窗口元素 (注意跨域资源共享的安全性问题)

In short: 简而言之:

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

Or access it at the server-side: 或在服务器端访问它:

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

read here 在这里阅读

How to set it hard-coded: 如何将其设置为硬编码:

Width of which element in the window you want to change? 您要更改窗口中哪个元素的宽度? body ? body div ? div

I guess you you're taking about the margin left & right of your page's body . 我猜您正在计算页面body左右两侧的margin

try insert the following style block to your head tag: 尝试在您的head标签中插入以下样式块:

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

where 10px is the left and right margin. 其中10px是左右边距。

You can also use 您也可以使用

window.resizeTo(x,y);

For Instance: 例如:

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

On onload() event of body call the function 在主体的onload()事件上调用函数

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

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

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