Reputation: 941
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.
I am using this code on buttonclick event.
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.
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. So i am printing the .aspx page in print.aspx. So i want this content should be in the static page width.
Upvotes: 0
Views: 10165
Reputation: 13003
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.
How to inject while opening:
Access child window elements from parent window using Javascript (Notice to security issues on Cross-origin resource sharing)
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 = ; //
How to set it hard-coded:
Width of which element in the window you want to change?
body
?div
?
I guess you you're taking about the margin
left & right of your page's body
.
try insert the following style block to your head
tag:
<style>
body{margin: 0 10px 0 10px;}
</style>
where 10px
is the left and right margin.
Upvotes: 1
Reputation: 5208
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
<body onload="resizeWindow()" >
body of page
</body>
Upvotes: 1
Reputation: 5563
You cannot change width of the page using window.open
, it only sets the window properties.
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
var newwindow = window.open('Print.aspx');
var elem = newwindow.document.getElementById('my-id');
elem.style.width = 'XYZpx'
P.S. :- The above code will only work if the new window has the same protocol, domain and port. If it's on another domain, you can't do this for security reasons.
Upvotes: 1
Reputation: 16698
Width specified inside window.open() function will only set width of the popup window. If you want to change the width of the page then you should try changing the width inside Print.aspx.
Upvotes: 1