最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to set the page size while opening the window? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Oct 3, 2012 at 9:25 Giri asked Oct 3, 2012 at 8:47 GiriGiri 94112 gold badges31 silver badges51 bronze badges 1
  • it doesn't really matter, you can change it from out side using JS like explained in my answer. – Yair Nevet Commented Oct 3, 2012 at 9:41
Add a ment  | 

4 Answers 4

Reset to default 1

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.

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.

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>

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 = ; //

read here

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.

发布评论

评论列表(0)

  1. 暂无评论