Search code examples
c#javascriptasp.netregisterclientscriptblockclientscript

Fetch JS return value in server side page_load event in asp.net


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.

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.

Till now I did the following: In child.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">

    <script language="javascript" type="text/javascript">
    function DoesParentExists()
    {
        var bool = (parent.location == window.location)? false : true;
        var HClientID ='<%=hfDoesParentExist.ClientID%>'; 
        document.getElementById(HClientID).Value = bool;
    }        
    </script>
    <div>        
        <h2>Content - In IFrame</h2>
        <asp:HiddenField runat="server" id="hfDoesParentExist" />
        <asp:CheckBox ID="chkValid" runat="server" />
        <asp:ImageButton ID="ImageButton_FillW8Online" ImageUrl="~/images/expand.gif"
        OnClick="btnVerify_Click" runat="server" style="height: 11px" />    
    </div>
</asp:Content>

in client.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DoesParentExists", "DoesParentExists()", true);
    if (hfDoesParentExist.Value == "true")
    {
        chkValid.Visible = false;
    }
}

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?


Solution

  • This line:

    document.getElementById(HClientID).Value = bool;
    

    Should be: (lower case value)

    document.getElementById(HClientID).value = bool;
    

    Also you cannot check the value of a hidden field set by javascript register callback, in the current executing context on the server side.

    I would move the logic to the client side to hide or show the checkbox. If the field must indeed be removed from the page you can do that as well with javascript.

    function DoesParentExists()
    {
        var bool = (parent.location == window.location)? false : true;
        var cehckboxId ='<%=chkValid.ClientID%>'; 
        if(bool){ 
            document.getElementById(cehckboxId).style.display = 'none';
        }
        else {
            document.getElementById(cehckboxId).style.display = 'block';
        }
    }    
    

    You may want to wrap the checkbox with a div and hide the container also to include the label.