Detecting screen resolution in server code?

  • Thread starter Bjorn Sagbakken
  • Start date
B

Bjorn Sagbakken

Can screen resolution be detected with server code (in Page_Load) ?

I have tried this:
public int Width =
Convert.ToInt32(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width);
public int Height =
Convert.ToInt32(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height);

and sure enough, I get a couple of numbers; Width=1024 and Height=738 when
running at 1024x768 - but I also get the same numbers when running at
1280x1024 (?) - so I just don't know what I'm actually reading here.

The idea is to rescale some controls to make the best out of the actual
resolution, and if I could do this server side I would be happy. But if
client script is the only way, well then that's the way.

Anyone with an opinion on this?

Bjorn
 
N

Nanda Lella[MSFT]

Have you tried something like

int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

--------------------
NNTP-Posting-Date: Tue, 04 Dec 2007 11:26:49 -0600
From: "Bjorn Sagbakken" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Detecting screen resolution in server code?
Date: Tue, 4 Dec 2007 18:26:56 +0100
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
Message-ID: <[email protected]>
Lines: 21
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 84.202.85.158
X-Trace: sv3-Ji2rwuV7a67ups7LW5TgTZncplX98gcFY7uxwSCxqsZxDD4F212nilD+r3le+Hq/XjkfT+ZZ
xIVbmJ/!/4vLCgW8rON3o+7LgZgaWKh5DOEtuQ4cRwLQvt3aOPSqWv3SILfTjXbmIOVdAvwC0JRS
imztRLko
X-Complaints-To: (e-mail address removed)
X-DMCA-Complaints-To: (e-mail address removed)
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.36
Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTFEEDS02.phx.gbl!news-out.
cwix.com!newsfeed.cwix.com!newsfeed.gamma.ru!Gamma.RU!newsfeed.icl.net!newsf
eed.fjserv.net!colt.net!feeder.news-service.com!xlned.com!feeder1.xlned.com!
amsnews11.chello.com!feeder1.cambrium.nl!feeder5.cambrium.nl!feed.tweaknews.
nl!63.218.45.11.MISMATCH!nx02.iad01.newshosting.com!newshosting.com!216.196.
98.140.MISMATCH!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp
..dca.giganews.com!nntp.telenor.com!news.telenor.com.POSTED!not-for-mail
Xref: TK2MSFTNGHUB02.phx.gbl microsoft.public.dotnet.framework.aspnet:52379
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Can screen resolution be detected with server code (in Page_Load) ?

I have tried this:
public int Width =
Convert.ToInt32(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width );
public int Height =
Convert.ToInt32(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Heigh t);

and sure enough, I get a couple of numbers; Width=1024 and Height=738 when
running at 1024x768 - but I also get the same numbers when running at
1280x1024 (?) - so I just don't know what I'm actually reading here.

The idea is to rescale some controls to make the best out of the actual
resolution, and if I could do this server side I would be happy. But if
client script is the only way, well then that's the way.

Anyone with an opinion on this?

Bjorn

--

Thank You,
Nanda Lella,

This Posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Juan T. Llibre

The problem, of course, is that using server code to determine screen resolution
will result in the *server* screen resolution being grabbed/returned.

I'd use Javascript to do that client-side.

<script language='javascript'>
function ClientScreenInfo()
{
var = window.screen.width;
var2 = window.screen.height;
}
</script>

....and store the values in hidden fields which you can manipulate in code.
 
B

Bjorn Sagbakken

Nanda Lella said:
Have you tried something like

int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

Yes, now I have. I get the same result as before.
But when I read the other answers, I realize this would only give me the
resolution on the server, not on the client.
So a client script is probably the neccessary approach anyway.

Bjorn
 
B

Bjorn Sagbakken

Eliyahu Goldin said:
Do you mean the resolution on the client machine? You can get in on client
side with javascript as window.screen.width and window.screen.height and
pass these values to server side in hidden input controls.

Very good idea. Thanks a lot.

Bjorn
 
B

Bjorn Sagbakken

Mark Rae said:
There is no other option...

Right. So now this works very well:
Height=window.screen.height
I write the height/width into a hidden field. Upon the first UpdatePanel
postback this value is stored in a session variable so I can re-use this on
various actions. Great.

Bjorn
 
B

Bjorn Sagbakken

Mark Rae said:
There is no other option...

Just one more issue:
Ideally I want to dynamically detect the actual client browser size if this
isn't in maximized mode.
I tried "availHeight" instead of height, but the result is not quite clear
(?)
Any tip?

Bjorn
 
M

Mark Rae [MVP]

Thanks for a 1400 pages manual. I'm sure it't useful in many ways.
At a glance, I just didn't find anything regarding the current subject
hovewer....

The link I gave you takes you straight to page 570, where you will find the
code that you require - did you read it...?
 
B

Bjorn Sagbakken

Mark Rae said:
The link I gave you takes you straight to page 570, where you will find
the code that you require - did you read it...?

I am sorry, Mark. I went straight to the top of the document to see what
kind of file this was, and started to browse the index. I just didn't
realize that you even had opened the book for me at the right page.

So it is hereby proven what they say: You can lead a horse to the water, but
you cannot make it drink.

(Thanks again)

Bjorn
 
L

Larry Bud

Right. So now this works very well:
Height=window.screen.height
I write the height/width into a hidden field. Upon the first UpdatePanel
postback this value is stored in a session variable so I can re-use this on
various actions. Great.

You will also have to update the values when the user changes the
browser size.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top