title: Fetch JS return value in server side page_load event in asp.net
body: <p>I have an aspx master/content page scenario. The parent page has an IFrame which points to a child.aspx. The child.aspx has a checkbox, On page_load of child.aspx, I want to show/hide the checkbox depending on the following logic:
  - if the child.aspx is opened directly, then I have to show the checkbox.
  - if the child.aspx is opened in the IFrame, then I have to hide the checkbox.
  Basically, I want to check in child.aspx, if it contains a parent window then hide the checkbox control otherwise show it.</p>
  
  <p>I will prefer the show/hide code in codebehind in Page_load event as I have to execute some more logic depending on whether the it is opened from parent window or not.</p>
  
  <p>Till now I did the following:
  In  child.aspx</p>
  
  <pre><code>&lt;asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server"&gt;
  
      &lt;script language="javascript" type="text/javascript"&gt;
      function DoesParentExists()
      {
          var bool = (parent.location == window.location)? false : true;
          var HClientID ='&lt;%=hfDoesParentExist.ClientID%&gt;'; 
          document.getElementById(HClientID).Value = bool;
      }        
      &lt;/script&gt;
      &lt;div&gt;        
          &lt;h2&gt;Content - In IFrame&lt;/h2&gt;
          &lt;asp:HiddenField runat="server" id="hfDoesParentExist" /&gt;
          &lt;asp:CheckBox ID="chkValid" runat="server" /&gt;
          &lt;asp:ImageButton ID="ImageButton_FillW8Online" ImageUrl="~/images/expand.gif"
          OnClick="btnVerify_Click" runat="server" style="height: 11px" /&gt;    
      &lt;/div&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>in client.aspx.cs</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DoesParentExists", "DoesParentExists()", true);
      if (hfDoesParentExist.Value == "true")
      {
          chkValid.Visible = false;
      }
  }
  </code></pre>
  
  <p>Using RegisterClientScriptBlock, I get error in JS. That the object hfDoesParentExist doesn't exist 'coz the control is not yet created. Right? I tried using RegisterStartupScript but in codebehind I always get null in hidden variable. I don't want to use the on button click or something like it. I need it on page_load event only. How to resolve the issue?</p>