Have resW=screen.wid th; resH=screen.hei ght in javascript. How can I read
these values in ASP.NET source code - Page_Load function of code behind?
Any suggestions?
Nov 19 '05
18 11648
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.EventArg s) Handles MyBase.Load
Try
'---Every page load
If Not IsPostBack Then
'---First page load only
Dim StringBuilder As New System.Text.Str ingBuilder
With StringBuilder
.Append("<scrip t language=""java script"">" & vbCrLf)
.Append("docume nt.getElementBy Id('resW').valu e =
window.screen.w idth;" & vbCrLf)
.Append("docume nt.getElementBy Id('resH').valu e =
window.screen.h eight;" & vbCrLf)
.Append("</script>" & vbCrLf)
End With
Page.RegisterSt artupScript("Sc reenSize",
StringBuilder.T oString)
Else
'---Post back only
Dim Width As Int32 = CType(Request.F orm("resW"), Int32)
Dim Height As Int32 = CType(Request.F orm("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
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message
news:ED******** *************** ***********@mic rosoft.com... 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. Justin Gengo" wrote:
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.Html Controls.HtmlGe nericControl
(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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:9C******** *************** ***********@mic rosoft.com... > I've have the following in HTML code... > <script language="javas cript"> > document.getEle mentById('resW' ) = window.screen.w idth; > document.getEle mentById('resH' ) = window.screen.h eight; > </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("r esW") or resW.Value? > > What am I missing? > > "Eliyahu Goldin" wrote: > >> 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 >> >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> news:BC******** *************** ***********@mic rosoft.com... >> > Have resW=screen.wid th; resH=screen.hei ght in javascript. How can I >> > read >> > these values in ASP.NET source code - Page_Load function of code >> > behind? >> > >> > Any suggestions? >> >> >>
<HEAD>
For the header description, i've used all sorts of variations as follow -
<script language="javas cript">
//document.getEle mentById('resW' ) = window.screen.w idth;
//document.getEle mentById('resH' ) = window.screen.h eight;
alert(window.sc reen.width);
alert(window.sc reen.height);
//resW.value = window.screen.w idth;
//resH.value = window.screen.h eight;
document.Form1. resW.value = screen.width;
document.Form1. resH.value = screen.height;
</script>
</HEAD>
<BODY bottomMargin="5 " background="ima ges\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.Val ue
_Width=Request. Form("resW")
Both return null strings.
I've added the following in page load for button control...
btn_V.Attribute s.Add("onclick" , "javascript :doc ument:form1.sub mit()" )
Any help here? Please advise
"S. Justin Gengo" wrote: 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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:CE******** *************** ***********@mic rosoft.com...I still get nothing for resW.value!
"Eliyahu Goldin" wrote:
Change to:
document.getEle mentById('resW' ).value = window.screen.w idth; document.getEle mentById('resH' ).value = window.screen.h eight;
and use resW.Value.
Eliyahu
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:9C******** *************** ***********@mic rosoft.com... > I've have the following in HTML code... > <script language="javas cript"> > document.getEle mentById('resW' ) = window.screen.w idth; > document.getEle mentById('resH' ) = window.screen.h eight; > </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("r esW") or resW.Value? > > What am I missing? > > "Eliyahu Goldin" wrote: > > > 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 > > > > "DavidS" <Da****@discuss ions.microsoft. com> wrote in message > > news:BC******** *************** ***********@mic rosoft.com... > > > Have resW=screen.wid th; resH=screen.hei ght in javascript. How can > > > I read > > > these values in ASP.NET source code - Page_Load function of code behind? > > > > > > Any suggestions? > > > > > >
David,
Did you see my post above with a working example?
--
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message
news:2B******** *************** ***********@mic rosoft.com... <HEAD> For the header description, i've used all sorts of variations as follow - <script language="javas cript"> //document.getEle mentById('resW' ) = window.screen.w idth; //document.getEle mentById('resH' ) = window.screen.h eight; alert(window.sc reen.width); alert(window.sc reen.height); //resW.value = window.screen.w idth; //resH.value = window.screen.h eight; document.Form1. resW.value = screen.width; document.Form1. resH.value = screen.height; </script> </HEAD> <BODY bottomMargin="5 " background="ima ges\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.Val ue _Width=Request. Form("resW")
Both return null strings.
I've added the following in page load for button control... btn_V.Attribute s.Add("onclick" , "javascript:doc ument:form1.sub mit()" )
Any help here? Please advise "S. Justin Gengo" wrote:
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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:CE******** *************** ***********@mic rosoft.com... >I still get nothing for resW.value! > > "Eliyahu Goldin" wrote: > >> Change to: >> >> document.getEle mentById('resW' ).value = window.screen.w idth; >> document.getEle mentById('resH' ).value = window.screen.h eight; >> >> and use resW.Value. >> >> Eliyahu >> >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> news:9C******** *************** ***********@mic rosoft.com... >> > I've have the following in HTML code... >> > <script language="javas cript"> >> > document.getEle mentById('resW' ) = window.screen.w idth; >> > document.getEle mentById('resH' ) = window.screen.h eight; >> > </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("r esW") or resW.Value? >> > >> > What am I missing? >> > >> > "Eliyahu Goldin" wrote: >> > >> > > 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 >> > > >> > > "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> > > news:BC******** *************** ***********@mic rosoft.com... >> > > > Have resW=screen.wid th; resH=screen.hei ght in javascript. How >> > > > can >> > > > I >> read >> > > > these values in ASP.NET source code - Page_Load function of code >> behind? >> > > > >> > > > Any suggestions? >> > > >> > > >> > > >> >> >>
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.getEle mentsByTag('res W')[0].value = window.screen.w idth;
document.getEle mentsByTag('res H')[0].value = window.screen.h eight;
</script
in the code behind in OnLoad
if (!IsPostBack)
{
RegisterStartup Script("<script >document.for ms['Form1'].submit();</script>");
}
else
{
string resW = Request.Form["resW"];
string resH = Request.Form["resH"];
}
-- bruce (sqlwork.com)
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message
news:CE******** *************** ***********@mic rosoft.com... I still get nothing for resW.value!
"Eliyahu Goldin" wrote:
Change to:
document.getEle mentById('resW' ).value = window.screen.w idth; document.getEle mentById('resH' ).value = window.screen.h eight;
and use resW.Value.
Eliyahu
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:9C******** *************** ***********@mic rosoft.com... > I've have the following in HTML code... > <script language="javas cript"> > document.getEle mentById('resW' ) = window.screen.w idth; > document.getEle mentById('resH' ) = window.screen.h eight; > </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("r esW") or resW.Value? > > What am I missing? > > "Eliyahu Goldin" wrote: > > > 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 > > > > "DavidS" <Da****@discuss ions.microsoft. com> wrote in message > > news:BC******** *************** ***********@mic rosoft.com... > > > Have resW=screen.wid th; resH=screen.hei ght in javascript. How can > > > I read > > > these values in ASP.NET source code - Page_Load function of code behind? > > > > > > Any suggestions? > > > > > >
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("r esW") and Request.Form("r esH"). 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("r esW"), Request.Form("r esH")
doesn't work.
3) why does the source block I've submitted earlier not work too.
Thanks always,
David
"S. Justin Gengo" wrote: 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.EventArg s) Handles MyBase.Load Try '---Every page load
If Not IsPostBack Then '---First page load only Dim StringBuilder As New System.Text.Str ingBuilder With StringBuilder .Append("<scrip t language=""java script"">" & vbCrLf) .Append("docume nt.getElementBy Id('resW').valu e = window.screen.w idth;" & vbCrLf) .Append("docume nt.getElementBy Id('resH').valu e = window.screen.h eight;" & vbCrLf) .Append("</script>" & vbCrLf) End With
Page.RegisterSt artupScript("Sc reenSize", StringBuilder.T oString) Else '---Post back only Dim Width As Int32 = CType(Request.F orm("resW"), Int32) Dim Height As Int32 = CType(Request.F orm("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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:ED******** *************** ***********@mic rosoft.com... 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. Justin Gengo" wrote:
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.Html Controls.HtmlGe nericControl
(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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:9C******** *************** ***********@mic rosoft.com... > I've have the following in HTML code... > <script language="javas cript"> > document.getEle mentById('resW' ) = window.screen.w idth; > document.getEle mentById('resH' ) = window.screen.h eight; > </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("r esW") or resW.Value? > > What am I missing? > > "Eliyahu Goldin" wrote: > >> 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 >> >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> news:BC******** *************** ***********@mic rosoft.com... >> > Have resW=screen.wid th; resH=screen.hei ght in javascript. How can I >> > read >> > these values in ASP.NET source code - Page_Load function of code >> > behind? >> > >> > Any suggestions? >> >> >>
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
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message
news:D7******** *************** ***********@mic rosoft.com... 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("r esW") and Request.Form("r esH"). 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("r esW"), Request.Form("r esH") doesn't work. 3) why does the source block I've submitted earlier not work too.
Thanks always, David
"S. Justin Gengo" wrote:
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.EventArg s) Handles MyBase.Load Try '---Every page load
If Not IsPostBack Then '---First page load only Dim StringBuilder As New System.Text.Str ingBuilder With StringBuilder .Append("<scrip t language=""java script"">" & vbCrLf) .Append("docume nt.getElementBy Id('resW').valu e = window.screen.w idth;" & vbCrLf) .Append("docume nt.getElementBy Id('resH').valu e = window.screen.h eight;" & vbCrLf) .Append("</script>" & vbCrLf) End With
Page.RegisterSt artupScript("Sc reenSize", StringBuilder.T oString) Else '---Post back only Dim Width As Int32 = CType(Request.F orm("resW"), Int32) Dim Height As Int32 = CType(Request.F orm("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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:ED******** *************** ***********@mic rosoft.com... > 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. Justin Gengo" wrote: > >> 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.Html Controls.HtmlGe nericControl >> >> (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 >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> news:9C******** *************** ***********@mic rosoft.com... >> > I've have the following in HTML code... >> > <script language="javas cript"> >> > document.getEle mentById('resW' ) = window.screen.w idth; >> > document.getEle mentById('resH' ) = window.screen.h eight; >> > </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("r esW") or resW.Value? >> > >> > What am I missing? >> > >> > "Eliyahu Goldin" wrote: >> > >> >> 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 >> >> >> >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> >> news:BC******** *************** ***********@mic rosoft.com... >> >> > Have resW=screen.wid th; resH=screen.hei ght in javascript. How >> >> > can I >> >> > read >> >> > these values in ASP.NET source code - Page_Load function of code >> >> > behind? >> >> > >> >> > Any suggestions? >> >> >> >> >> >> >> >> >>
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.getEl ementById()"
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.
"S. Justin Gengo" wrote: 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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:D7******** *************** ***********@mic rosoft.com...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("r esW") and Request.Form("r esH"). 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("r esW"), Request.Form("r esH") doesn't work. 3) why does the source block I've submitted earlier not work too.
Thanks always, David
"S. Justin Gengo" wrote:
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.EventArg s) Handles MyBase.Load Try '---Every page load
If Not IsPostBack Then '---First page load only Dim StringBuilder As New System.Text.Str ingBuilder With StringBuilder .Append("<scrip t language=""java script"">" & vbCrLf) .Append("docume nt.getElementBy Id('resW').valu e = window.screen.w idth;" & vbCrLf) .Append("docume nt.getElementBy Id('resH').valu e = window.screen.h eight;" & vbCrLf) .Append("</script>" & vbCrLf) End With
Page.RegisterSt artupScript("Sc reenSize", StringBuilder.T oString) Else '---Post back only Dim Width As Int32 = CType(Request.F orm("resW"), Int32) Dim Height As Int32 = CType(Request.F orm("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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:ED******** *************** ***********@mic rosoft.com... > 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. Justin Gengo" wrote: > >> 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.Html Controls.HtmlGe nericControl >> >> (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 >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> news:9C******** *************** ***********@mic rosoft.com... >> > I've have the following in HTML code... >> > <script language="javas cript"> >> > document.getEle mentById('resW' ) = window.screen.w idth; >> > document.getEle mentById('resH' ) = window.screen.h eight; >> > </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("r esW") or resW.Value? >> > >> > What am I missing? >> > >> > "Eliyahu Goldin" wrote: >> > >> >> 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 >> >> >> >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> >> news:BC******** *************** ***********@mic rosoft.com... >> >> > Have resW=screen.wid th; resH=screen.hei ght in javascript. How >> >> > can I >> >> > read >> >> > these values in ASP.NET source code - Page_Load function of code >> >> > behind? >> >> > >> >> > Any suggestions? >> >> >> >> >> >> >> >> >>
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" wrote: 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.getEle mentsByTag('res W')[0].value = window.screen.w idth; document.getEle mentsByTag('res H')[0].value = window.screen.h eight; </script
in the code behind in OnLoad
if (!IsPostBack) { RegisterStartup Script("<script >document.for ms['Form1'].submit();</script>"); } else { string resW = Request.Form["resW"]; string resH = Request.Form["resH"]; }
-- bruce (sqlwork.com)
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:CE******** *************** ***********@mic rosoft.com...I still get nothing for resW.value!
"Eliyahu Goldin" wrote:
Change to:
document.getEle mentById('resW' ).value = window.screen.w idth; document.getEle mentById('resH' ).value = window.screen.h eight;
and use resW.Value.
Eliyahu
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:9C******** *************** ***********@mic rosoft.com... > I've have the following in HTML code... > <script language="javas cript"> > document.getEle mentById('resW' ) = window.screen.w idth; > document.getEle mentById('resH' ) = window.screen.h eight; > </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("r esW") or resW.Value? > > What am I missing? > > "Eliyahu Goldin" wrote: > > > 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 > > > > "DavidS" <Da****@discuss ions.microsoft. com> wrote in message > > news:BC******** *************** ***********@mic rosoft.com... > > > Have resW=screen.wid th; resH=screen.hei ght in javascript. How can > > > I read > > > these values in ASP.NET source code - Page_Load function of code behind? > > > > > > Any suggestions? > > > > > >
David,
Glad I could help, Have good one!
--
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
"DavidS" <Da****@discuss ions.microsoft. com> wrote in message
news:62******** *************** ***********@mic rosoft.com... 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.getEl ementById()" 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.
"S. Justin Gengo" wrote:
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 "DavidS" <Da****@discuss ions.microsoft. com> wrote in message news:D7******** *************** ***********@mic rosoft.com... >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("r esW") and Request.Form("r esH"). 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("r esW"), Request.Form("r esH") > doesn't work. > 3) why does the source block I've submitted earlier not work too. > > Thanks always, > David > > "S. Justin Gengo" wrote: > >> 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.EventArg s) Handles MyBase.Load >> Try >> '---Every page load >> >> If Not IsPostBack Then >> '---First page load only >> Dim StringBuilder As New System.Text.Str ingBuilder >> With StringBuilder >> .Append("<scrip t language=""java script"">" & >> vbCrLf) >> .Append("docume nt.getElementBy Id('resW').valu e = >> window.screen.w idth;" & vbCrLf) >> .Append("docume nt.getElementBy Id('resH').valu e = >> window.screen.h eight;" & vbCrLf) >> .Append("</script>" & vbCrLf) >> End With >> >> Page.RegisterSt artupScript("Sc reenSize", >> StringBuilder.T oString) >> Else >> '---Post back only >> Dim Width As Int32 = CType(Request.F orm("resW"), >> Int32) >> Dim Height As Int32 = CType(Request.F orm("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 >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> news:ED******** *************** ***********@mic rosoft.com... >> > 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. Justin Gengo" wrote: >> > >> >> 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.Html Controls.HtmlGe nericControl >> >> >> >> (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 >> >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> >> news:9C******** *************** ***********@mic rosoft.com... >> >> > I've have the following in HTML code... >> >> > <script language="javas cript"> >> >> > document.getEle mentById('resW' ) = window.screen.w idth; >> >> > document.getEle mentById('resH' ) = window.screen.h eight; >> >> > </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("r esW") or resW.Value? >> >> > >> >> > What am I missing? >> >> > >> >> > "Eliyahu Goldin" wrote: >> >> > >> >> >> 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 >> >> >> >> >> >> "DavidS" <Da****@discuss ions.microsoft. com> wrote in message >> >> >> news:BC******** *************** ***********@mic rosoft.com... >> >> >> > Have resW=screen.wid th; resH=screen.hei ght in javascript. How >> >> >> > can I >> >> >> > read >> >> >> > these values in ASP.NET source code - Page_Load function of >> >> >> > code >> >> >> > behind? >> >> >> > >> >> >> > Any suggestions? >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ZaGras |
last post by:
when my vb program run on another computer instead of my computer, the
user interface is biggger..this is because of the screen resolution...
anyone know how to solve pls?
|
by: Dufe |
last post by:
Hello all:
To deal with the problem of differing user screen resolutions, I've
explored: 1) making the pages in PHP, 2) having different pages on the
same page and selecting the proper one via JavaScript, and 3) using
fancy redirects and forced "back skip" redirects with cookies. Every
approach has some fatal flaw as far as I have been able to persue it.
My most recent idea is to make multiple style sheets (selectable via
javascript...
|
by: Chris |
last post by:
After exhausting my search on the MS website, I can't find a straight answer.
I fin dit hard to believe that MS left our something so useful in ASP.NET as
screen resolution detection.
Problem:
I would like to detect, get values for, the screen resolution in px but in a
code behind. Is this possible?
There is a System.Windows.Forms.Screen class for the Windows client side,
why are there none for browser/ASP.NET client?
|
by: pjac |
last post by:
I need some help with some VB language that will change the screen
resolution on a monitor when a MS-Access 2000 database is opened from
1024 x 768 to 800 x 600.
Any help with this effort would be deeply appreciated.
Thanks in advance....
|
by: fabrice |
last post by:
Hi,
I'm trying to get the screen resolution of the client and to realize a
treatment in code behind.
it seem to be hard.
I can get information about the resolution with this following sub, call in
Page_Load.I fix the text of The Control Label. But no treatment is possible.
If i try to keep out the text of this label in code behind, i have nothing.
The variable is empty.
| |
by: Darian |
last post by:
I am wondering how (if it is possible of course) I can change a users
screen resolution to a certain size when running my application and
then change it back when they exit my application.
For instance, if the users screen size is 800x600, I want the
resolution to change to 1260x780 when running my program and then
change back to 800x600 when exiting my program.
Thanks,
Darian
|
by: Maxi |
last post by:
I have a 30X16 cells table in my html page. Table height and width are
set to 100%. I have set size of every cell inside the table to 24
pixel.
When I open the html page in maximize state in either resolution 800 X
600 or 1152 X 864 it takes up the entire explorer size.
In screen resolution 800 X 600, if I insert a pictures of 24 X 24 pixel
in the cells it takes up the entire cell size (ideally it should as the
cell size is also 24 X...
|
by: Steve Wright |
last post by:
Hi
I'm developing a webpage that needs to include a different stylesheet
depending on the screen resolution.
I know that I could read the resolution in javascript and then do some
sort of stylesheet switcher as part of the onload event but I would
rather link in the correct stylesheet for the resolution in the first
place.
|
by: nasima khan |
last post by:
Hi,
i am nasima. I have got a code for setting the screen resolution of my page, but i am unable to understand. Can any one give a complete data explanation of the below code.
Sub ChangeRes(X As Long, Y As Long, Bits As Long)
Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
'Get the info into DevM
erg = EnumDisplaySettings(0&, 0&, DevM)
'This is what we're going to change
DevM.dmFields =...
|
by: =?Utf-8?B?UmljaA==?= |
last post by:
A lot of users at my workplace use different screen resolutions, and I build
apps to use 1680 x 1050 pixels res by default. But some users are using 800
x 600, and the apps are too large for their screen.
I used to write code in Java a few years ago (2005), and you could stretch a
form with the mouse and all the controls and fonts would resize to larger or
smaller size. Does .Net framework 3.5 support this kind of functionality?
Or -...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |