2011年7月6日 星期三

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?

沒有留言:

張貼留言