Home All Groups Group Topic Archive Search About

Brwoser and screen width?

Author
4 Jul 2009 10:43 PM
John Baima
I'd like to know how wide the browser window is at any given time as
well as the screen width with ASP.NET. Is there an easy way to do
that? Thanks.

-John
John Baima

Author
4 Jul 2009 11:15 PM
Mark Rae [MVP]
"John Baima" <john@nospam.com> wrote in message
news:6nmv451h5kfu679d2qmelfhkig6q987vl2@4ax.com...

> I'd like to know how wide the browser window is at any given time as
> well as the screen width with ASP.NET. Is there an easy way to do
> that?

This is a property of the browser which isn't passed to the server as part
of the request's server variables collection. This means that it's not
available server-side directly.

However, you can retrieve this via client-side JavaScript. This is a bit of
an inexact science, as different browsers use different properties for this
information, e.g.

screen.width
screen.availWidth
window.innerWidth
document.documentElement.clientWidth
document.body.clientWidth

If you need these values server-side, your only option is to populate hidden
fields with their values so that they can be inspected during postbacks...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Are all your drivers up to date? click for free checkup

Author
5 Jul 2009 1:46 AM
John Baima
Show quote Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote:
>> I'd like to know how wide the browser window is at any given time as
>> well as the screen width with ASP.NET. Is there an easy way to do
>> that?
>
>This is a property of the browser which isn't passed to the server as part
>of the request's server variables collection. This means that it's not
>available server-side directly.
>
>However, you can retrieve this via client-side JavaScript. This is a bit of
>an inexact science, as different browsers use different properties for this
>information, e.g.
>
>screen.width
>screen.availWidth
>window.innerWidth
>document.documentElement.clientWidth
>document.body.clientWidth
>
>If you need these values server-side, your only option is to populate hidden
>fields with their values so that they can be inspected during postbacks...

Do you have any examples of this? I do mostly winform programming but
I would have thought that this would be something that lots of people
would be interested in.

-John

John Baima
Author
5 Jul 2009 5:34 AM
Juan T. Llibre
re:
!> Do you have any examples of this?

http://www.google.com/search?hl=en&q=screen.width

re:
!> I would have thought that this would be something that lots of people would be interested in.

They are...




Juan T. Llibre, asp.net MVP
¿ Estas probando VS 2010 y ASP.NET 4.0 ?
Regístrate (gratis) en los Foros de VS 2010 y ASP.NET 4.0, en español
http://asp.net.do/foros/forums/
=====================================================
Show quoteHide quote
"John Baima" <john@nospam.com> wrote in message news:db10555jmuhbkghue4v3ei7v04tkpfig72@4ax.com...
> "Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote:
>>> I'd like to know how wide the browser window is at any given time as
>>> well as the screen width with ASP.NET. Is there an easy way to do
>>> that?
>>
>>This is a property of the browser which isn't passed to the server as part
>>of the request's server variables collection. This means that it's not
>>available server-side directly.
>>
>>However, you can retrieve this via client-side JavaScript. This is a bit of
>>an inexact science, as different browsers use different properties for this
>>information, e.g.
>>
>>screen.width
>>screen.availWidth
>>window.innerWidth
>>document.documentElement.clientWidth
>>document.body.clientWidth
>>
>>If you need these values server-side, your only option is to populate hidden
>>fields with their values so that they can be inspected during postbacks...
>
> Do you have any examples of this? I do mostly winform programming but
> I would have thought that this would be something that lots of people
> would be interested in.
>
> -John
>
> John Baima
Author
5 Jul 2009 9:34 AM
Mark Rae [MVP]
"John Baima" <john@nospam.com> wrote in message
news:db10555jmuhbkghue4v3ei7v04tkpfig72@4ax.com...

> Do you have any examples of this?

Depends what you intend to do with the information, and whether you need to
know it client-side or server-side...

Can you provide a bit more information about what you're trying to do...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
5 Jul 2009 11:23 AM
John Baima
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote:
>Depends what you intend to do with the information, and whether you need to
>know it client-side or server-side...
>
>Can you provide a bit more information about what you're trying to do...?

I need it server side. I have several text items in a database that I
want to display and I want to get as many columns as possible in a
table, but I don't want the user to have to scroll right and left. So,
I have to know how wide each of the elements are and how much screen
I have to work with.

I did get the scheme of:

http://www.devcity.net/Articles/64/1/screenresolution.aspx

working and extended it to what I need. Now I just need to know the
width of text elements :-)

-John

John Baima
Author
5 Jul 2009 11:40 AM
Mark Rae [MVP]
"John Baima" <john@nospam.com> wrote in message
news:7p2155175edhbjoq80uhdcral0snjp87q0@4ax.com...

> I did get the scheme of:
>
> http://www.devcity.net/Articles/64/1/screenresolution.aspx
>
> working and extended it to what I need.

Ignore that page completely. It is *seriously* out-of-date, e.g. <script
language="javascript"> - the language tag of the <script /> element has been
deprecated for nearly THIRTEEN years!!!

Did you read all of the comments at the bottom of the article? Specifically,
what happens if the user changes their screen resolution?

Apart from that, obviously detecting the client machine's screen resolution
(e.g. 1280*1024) does not tell you how the width of the client browser
unless it is maximised - that's why you need to know the current width of
the browser, not the width of the screen...


> I need it server side. I have several text items in a database that I
> want to display and I want to get as many columns as possible in a
> table, but I don't want the user to have to scroll right and left.

But that's the way web browsers work! Users will be well used to this sort
of behaviour...


> So, I have to know how wide each of the elements are and how much screen
> I have to work with.

Not possible server-side. The reason is that, unless you explicitly set the
width of DOM elements, the web browser will do this automatically as its
parsing the HTML stream response being received from the webserver. And, by
that time, it's too late for the server to do anything to the HTML stream...

Your best bet would be to set the widths of the controls explicitly and let
the contents wrap...

I really do think you're making things unnecessarily difficult for
yourself...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Bookmark and Share