简体   繁体   English

在ASP.NET的服务器端page_load事件中获取JS返回值

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

I have an aspx master/content page scenario. 我有一个aspx母版/内容页面方案。 The parent page has an IFrame which points to a child.aspx. 父页面上有一个指向child.aspx的IFrame。 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. child.aspx有一个复选框,在child.aspx的page_load上,我想根据以下逻辑显示/隐藏该复选框:-如果child.aspx直接打开,则必须显示该复选框。 - if the child.aspx is opened in the IFrame, then I have to hide the checkbox. -如果在IFrame中打开child.aspx,则必须隐藏该复选框。 Basically, I want to check in child.aspx, if it contains a parent window then hide the checkbox control otherwise show it. 基本上,我想签入child.aspx,如果它包含父窗口,则隐藏复选框控件,否则将其显示。

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. 我将更喜欢在Page_load事件中的代码后面的显示/隐藏代码,因为我必须执行更多逻辑,具体取决于是否从父窗口打开它。

Till now I did the following: In child.aspx 到目前为止,我已经执行了以下操作:在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 在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. 使用RegisterClientScriptBlock,我在JS中出错。 That the object hfDoesParentExist doesn't exist 'coz the control is not yet created. 对象hfDoesParentExist不存在,因为尚未创建控件。 Right? 对? I tried using RegisterStartupScript but in codebehind I always get null in hidden variable. 我尝试使用RegisterStartupScript,但在代码隐藏中,隐藏变量始终为null。 I don't want to use the on button click or something like it. 我不想使用按钮点击或类似的东西。 I need it on page_load event only. 我只在page_load事件上需要它。 How to resolve the issue? 该如何解决?

This line: 这行:

document.getElementById(HClientID).Value = bool;

Should be: (lower case value ) 应为:(小写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. 同样,您无法在服务器端当前正在执行的上下文中检查由javascript register回调设置的隐藏字段的值。

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. 如果确实必须从页面中删除该字段,则也可以使用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. 您可能要用div包裹复选框,并隐藏容器以包含标签。

To do it server-side, I would rely on a querystring parameter. 要在服务器端做到这一点,我将依靠querystring参数。 Have the parent page load the child page by appending ?inframe=1 . 通过附加?inframe=1使父页面加载子页面。 Then check for that value in your Page_Load . 然后在Page_Load检查该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM