screen resolution

G

Guest

Have resW=screen.width; resH=screen.height in javascript. How can I read
these values in ASP.NET source code - Page_Load function of code behind?

Any suggestions?
 
G

Guest

I've have the following in HTML code...
<script language="javascript">
document.getElementById('resW') = window.screen.width;
document.getElementById('resH') = window.screen.height;
</script>
<input type=hidden name=resW id=resW runat=server>
<input type=hidden name=resH id=resH runat=server>

How can I access the values in ASP.NET (VB) code behind? I keep getting
null values when I use Request.Form("resW") or resW.Value?

What am I missing?
 
S

S. Justin Gengo

David,

I can think of two things that may be happening.

The first is that there won't be any value in either of those fields until a
postback has occurred. The first time your code runs the page hasn't
displayed on the client and the javascript hasn't filled those fields.

The second (if the problem isn't the first) is that setting the controls as
runat="Server" may be interfering with the process. If you're going to use
Request.Form to get the values the controls don't have to be .Net controls.
You would only use runat="Server" if you want to reference the hidden inputs
as .Net server side controls. To do that you should declare them on your
page something like:

Protected WithEvents resW As System.Web.HtmlControls.HtmlGenericControl

(I typed that from memory, so double check the system declaration...)

But from the looks of things I would just remove the runat="Server" and then
do the Request.Form on postback only.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
F

Franck

DavidS said:
I've have the following in HTML code...
<script language="javascript">
document.getElementById('resW') = window.screen.width;
document.getElementById('resH') = window.screen.height;

have you tried
document.getElementById('resW').value = window.screen.width;
document.getElementById('resH').value = window.screen.height;
instead?
 
E

Eliyahu Goldin

You need to pass the values from client to server side. The standard way is
to use hidden input controls:

<input type=hidden runat=server id=inhResW>

Eliyahu
 
G

Guest

I still get nothing for resW.value!

Eliyahu Goldin said:
Change to:

document.getElementById('resW').value = window.screen.width;
document.getElementById('resH').value = window.screen.height;

and use resW.Value.

Eliyahu
 
S

S. Justin Gengo

David,

After you bring up your page on the client do a view source and check the
hidden inputs and see if the value exists there or not.

If it doesn't, show us how you're calling the initial javascript on page
load.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
G

Guest

Any example code I can review to get this to work is appreciated. I'd think
this wouldn't be complicated - but right now, I'm getting null values from
solutions recommended. Again, I simply want to read the screen resolution on
the client. In the ASP.NET code - after a postback, I simply then want to be
able to read these javascript values and do respective processing.

Please advise - I have 3 replies - and now seem confused by the solution
presented by all.
 
S

S. Justin Gengo

David,

Here's working code:

Form:

<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Post Back"></asp:Button>
<INPUT id="resW" type="hidden" name="resW">
<INPUT id="resH" type="hidden" name="resH">
</form>



Code Behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
'---Every page load

If Not IsPostBack Then
'---First page load only
Dim StringBuilder As New System.Text.StringBuilder
With StringBuilder
.Append("<script language=""javascript"">" & vbCrLf)
.Append("document.getElementById('resW').value =
window.screen.width;" & vbCrLf)
.Append("document.getElementById('resH').value =
window.screen.height;" & vbCrLf)
.Append("</script>" & vbCrLf)
End With

Page.RegisterStartupScript("ScreenSize",
StringBuilder.ToString)
Else
'---Post back only
Dim Width As Int32 = CType(Request.Form("resW"), Int32)
Dim Height As Int32 = CType(Request.Form("resH"), Int32)

Response.Write(Width.ToString & " X " & Height.ToString)
End If
'---Every page load
Catch ex As Exception
'---Handle exceptions here.
End Try
End Sub


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
E

Eliyahu Goldin

Change to:

document.getElementById('resW').value = window.screen.width;
document.getElementById('resH').value = window.screen.height;

and use resW.Value.

Eliyahu
 
G

Guest

<HEAD>
For the header description, i've used all sorts of variations as follow -
<script language="javascript">
//document.getElementById('resW') = window.screen.width;
//document.getElementById('resH') = window.screen.height;
alert(window.screen.width);
alert(window.screen.height);
//resW.value = window.screen.width;
//resH.value = window.screen.height;
document.Form1.resW.value = screen.width;
document.Form1.resH.value = screen.height;
</script>
</HEAD>
<BODY bottomMargin="5" background="images\horse_line.gif" topMargin="5"
MS_POSITIONING="GridLayout">
<FORM id="Form1" method="post" runat="server">
<input type=hidden name=resW id=resW runat=server>
<input type=hidden name=resH id=resH runat=server>
-----------------------------------------------------------------------------
I'm using a button control to post event, and then I attempt to review
variable information in code ---

_Width=resW.Value
_Width=Request.Form("resW")

Both return null strings.

I've added the following in page load for button control...
btn_V.Attributes.Add("onclick", "javascript:document:form1.submit()" )

Any help here? Please advise
 
B

Bruce Barker

you need to do the postback. try:

<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Post Back"></asp:Button>
<INPUT id="resW" type="hidden" name="resW">
<INPUT id="resH" type="hidden" name="resH">
</form>
<script>
document.getElementsByTag('resW')[0].value = window.screen.width;
document.getElementsByTag('resH')[0].value = window.screen.height;
</script

in the code behind in OnLoad

if (!IsPostBack)
{
RegisterStartupScript("<script>document.forms['Form1'].submit();</script>");
}
else
{
string resW = Request.Form["resW"];
string resH = Request.Form["resH"];
}


-- bruce (sqlwork.com)
 
G

Guest

I did use this code - and it does work, but there's one issue I'm seeing. In
the page load function, for the very first time, you call the javacode via
ScreenSize startup script. When no postbacks, the following is observed - i)
for first time (NO POSTBACKS), the values are present for
Request.Form("resW") and Request.Form("resH"). If I do a 2nd POSTBACK, then
the values are null. I have workaround using session variables - after 1st
post - but my questions are now as follows (for clarification - and for my
understanding)....
1) <input ... runat=server> runat=server not required since not posting on
events
2) why the 2nd postback, the Request.Form("resW"), Request.Form("resH")
doesn't work.
3) why does the source block I've submitted earlier not work too.

Thanks always,
David
 
S

S. Justin Gengo

David,

To answer your questions:

1) runat=server is only needed if you want to access a web control in the
code behind page. Since the hidden inputs are accessible via the Request
object there is no need for the overhead. Especially since you aren't really
using the inputs as dynamic controls.

2) It would work, if you run the javascript every time the page loads. Note
that I've placed the script inside of a If Not IsPostBack so the values for
the inputs are only being set the first time. The javascript setting them is
non-existent after the first page load. There is no need to keep running the
script because, as you've discovered on your own, it's more efficient to
just store the values in a session variable or viewstate since they won't
change.

3) Well, I can't be absolutely certain why your source block wasn't working.
It could be where you were inserting it on the page. It could be that it
didn't have the .value appended (but I assume you changed that based on
earlier posts). Or it could be that the way you were placing it on the page
it just wasn't getting fired. It's very difficult to say without seeing the
code for the entire page...

I hope these answers help. If you need any further clarification let me
know.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
G

Guest

Thank you again - you've resolved my problem - and answered my questions too.
Also, you've confirmed my understanding too in (1) and (2). As far as (3)
does - your's works - I'll review (when I have time), my section in more
detail with yours - as long as I know the "document.getElementById()"
function works (I was suspecting issue with it - since it was first time
used), but now that I have working version, I can experiment to find reason.

Again - you were awesome in getting answer to all my questions. Have great
day.
 
G

Guest

Thank you too. I had this answered above - but your response give me
variation and I'll review the best solution for my needs - or revise what I
have based on all inputs received.

Again, thanks to yourself and S. Justing Gengo - both very prompt and
professional solutions for me. Kind regards, always!

Bruce Barker said:
you need to do the postback. try:

<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Post Back"></asp:Button>
<INPUT id="resW" type="hidden" name="resW">
<INPUT id="resH" type="hidden" name="resH">
</form>
<script>
document.getElementsByTag('resW')[0].value = window.screen.width;
document.getElementsByTag('resH')[0].value = window.screen.height;
</script

in the code behind in OnLoad

if (!IsPostBack)
{
RegisterStartupScript("<script>document.forms['Form1'].submit();</script>");
}
else
{
string resW = Request.Form["resW"];
string resH = Request.Form["resH"];
}


-- bruce (sqlwork.com)


DavidS said:
I still get nothing for resW.value!
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top