Note that there are some explanatory texts on larger screens.

plurals
  1. POI am getting an error from getElementById for JAVA
    text
    copied!<p>I have been attempting to pass values to code behind and have gone through the forums to determine the best way to do this. A number of the answers suggested using a hidden field. I have been trying to do just that but keep getting an error when I debug saying that the JAVA variable is null when it retrieves the element by ID. I cannot seem to figure out why because I am using the exact code examples from the answers that were marked an the best solution and working correctly. I must be doing something wrong but I can't figure out what is is. Below is the code I borrowed from the forums for the ASPX page and the code behind.</p> <pre><code>***&lt;Page Code&gt;*** %@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"&gt; &lt;script type="text/javascript"&gt; { var hidden1 = document.getElementById('hidvalue') hidden1.value=window.innerWidth.toString &amp; "X" &amp; window.innerHeight.toString; } &lt;/script&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"&gt; &lt;input id="hidvalue" type="hidden" runat="server" /&gt; &lt;asp:Label ID="Label1" runat="server" Text=""&gt;&lt;/asp:Label&gt; &lt;/asp:Content&gt; ***&lt;Code Behind Page&gt;*** Partial Class test Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Label1.Text = hidvalue.Value End Sub End Class </code></pre> <p>The Error is thrown when the JAVA script gets to the line that starts with the code 'hidden1.value=window.innerWidth.toString'. The error says that it can't assign a value to the variable 'hidden1' because it is null. It should be populating with the element 'hidvalue' but it apparently is not and I cannot figure out why. Does anyone have an idea for me as to what I am doing wrong here?</p> <p>REVISION: Thanks to everyone's help in resolving the initial problem, which was fixed by adding OnLoad and ReSize to the page with code in each to enable these events to track the window size in the proper page load order. (See code below) For the test.aspx page, I am using a visible label to store the size so I can see the values as they change. The test.aspx child page is running under a basically blank master page and it displays the window size at load and displays a constant changing value when resizing perfectly.</p> <pre><code>***&lt;MASTER PAGE&gt;*** &lt;%@ Master Language="VB" AutoEventWireup="false" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;asp:ContentPlaceHolder ID="HeadContent" runat="server"/&gt; &lt;/head&gt; &lt;body&gt; &lt;form runat="server"&gt; &lt;div class="page"&gt; &lt;div class="main"&gt; &lt;asp:ContentPlaceHolder ID="MainContent" runat="server"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; ***&lt;TEST PAGE&gt;*** &lt;%@ Page Title="" Language="VB" MasterPageFile="~/Empty.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"&gt; &lt;script type="text/javascript"&gt; function attachEventHandlerResize(element, eventToHandle, eventHandler) { if (element.attachEvent) { element.attachEvent(eventToHandle, eventHandler); } else if (element.addEventListener) { element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false); } else { element[eventToHandle] = eventHandler; } } attachEventHandlerResize(window, "onresize", function () { var hidden1 = document.getElementById('&lt;%= LabelWindowSize.ClientID%&gt;'); hidden1.innerText=window.innerWidth + "X" + window.innerHeight; }); function attachEventHandlerLoad(element, eventToHandle, eventHandler) { if (element.attachEvent) { element.attachEvent(eventToHandle, eventHandler); } else if (element.addEventListener) { element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false); } else { element[eventToHandle] = eventHandler; } } attachEventHandlerLoad(window, "onload", function () { var hidden1 = document.getElementById('&lt;%= LabelWindowSize.ClientID%&gt;'); hidden1.innerText = window.innerWidth + "X" + window.innerHeight; }); &lt;/script&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"&gt; &lt;asp:Label ID="LabelWindowSize" runat="server"/&gt; &lt;/asp:Content&gt; ***&lt;CODE BEHIND TEST.ASPX&gt;*** Imports System.Diagnostics Partial Class test Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Debug.Print("LabelWindowSize.text value=" + LabelWindowSize.Text) End Sub End Class </code></pre> <p>The label on the page displays the values correctly in the browser (ie:1050X724). However, for some reason, the code behind can not access the displayed value when getting the Text from the label. The debug output is: 'LabelWindowSize.text value=' with no value, and a check of the text value for LabelWindowSize shows "".</p> <p>I have a hunch the problem is happening because of the order in which a page loads, executes, and accesses elements, much like the earlier solution resolved. Does anyone have an idea what is actually causing this problem?</p> <p>REVISION: Ok, changed the aspx page to use a hidden text control</p> <pre><code>&lt;%@ Page Title="" Language="VB" MasterPageFile="~/Empty.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"&gt; &lt;script type="text/javascript"&gt; function attachEventHandlerResize(element, eventToHandle, eventHandler) { if (element.attachEvent) { element.attachEvent(eventToHandle, eventHandler); } else if (element.addEventListener) { element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false); } else { element[eventToHandle] = eventHandler; } } attachEventHandlerResize(window, "onresize", function () { var hidden1 = document.getElementById('&lt;%= labelwindowsize.ClientID%&gt;'); hidden1.value = window.innerWidth + "X" + window.innerHeight; }); function attachEventHandlerLoad(element, eventToHandle, eventHandler) { if (element.attachEvent) { element.attachEvent(eventToHandle, eventHandler); } else if (element.addEventListener) { element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false); } else { element[eventToHandle] = eventHandler; } } attachEventHandlerLoad(window, "onload", function () { var hidden1 = document.getElementById('&lt;%= LabelWindowSize.ClientID%&gt;'); hidden1.value = window.innerWidth + "X" + window.innerHeight; }); &lt;/script&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"&gt; &lt;input id="labelwindowsize" type="hidden" runat="server"/&gt; &lt;/asp:Content&gt; </code></pre> <p>It is populating ok as you can see from the page inspector view:</p> <pre><code>&lt;form name="aspnetForm" id="aspnetForm" action="test.aspx" method="post"&gt;...&lt;/form&gt; &lt;div&gt;...&lt;/div&gt; &lt;/div&gt; &lt;div&gt;...&lt;/div&gt; &lt;/div&gt; &lt;div class="page"&gt;...&lt;/div&gt; &lt;div class="main"&gt;...&lt;/div&gt; &lt;input name="ctl00$MainContent$labelwindowsize" id="ctl00_MainContent_labelwindowsize" type="hidden" value="645X557"&gt;&lt;/input&gt; &lt;/input&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>However, the code behind still cannot see the value ... the following line produces "" for the 'value'</p> <pre><code>Debug.Print("LabelWindowSize.text value=" + labelwindowsize.Value) </code></pre> <p><strong>For a more detailed explanation:</strong> The purpose for the code I am trying to implement is to retrieve the browser window size in order to store the width and height in an App_Code module as global variables to be accessed by pages. The reason for wanting the global window size variables is to set the size of scrollable popups that open on several pages to the full size of the current window. I am trying to do this in order to display as much as I can of images and other large items like huge tables or grids that are much larger than the average display window. The reason I want to do this is to cut down on the amount of scrolling the user needs to do to view the entire contents of the scrollable popup window and to be able to resize the popup to the full window size when the user resizes the browser window. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload