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

发布于 2024-10-21 00:52:02 字数 1682 浏览 5 评论 0原文

我有一个 aspx 母版/内容页面场景。父页面有一个指向 child.aspx 的 IFrame。 child.aspx 有一个复选框,在 child.aspx 的 page_load 上,我想根据以下逻辑显示/隐藏该复选框: - 如果直接打开child.aspx,那么我必须显示该复选框。 - 如果 child.aspx 在 IFrame 中打开,那么我必须隐藏该复选框。 基本上,我想检查 child.aspx,如果它包含父窗口,则隐藏复选框控件,否则显示它。

我更喜欢在 Page_load 事件的代码隐藏中显示/隐藏代码,因为我必须根据是否从父窗口打开它来执行更多逻辑。

到目前为止我做了以下事情: 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>

在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;
    }
}

使用 RegisterClientScriptBlock,我在 JS 中遇到错误。对象 hfDoesParentExist 不存在,因为控件尚未创建。正确的?我尝试使用 RegisterStartupScript 但在代码隐藏中我总是在隐藏变量中得到 null 。我不想使用按钮单击或类似的操作。我只在 page_load 事件中需要它。如何解决问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初雪 2024-10-28 00:52:02

此行:

document.getElementById(HClientID).Value = bool;

应该是:(小写 value

document.getElementById(HClientID).value = bool;

此外,您无法在服务器端的当前执行上下文中检查由 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';
    }
}    

您可能想用 div 包裹复选框并隐藏容器以包含标签。

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.

风透绣罗衣 2024-10-28 00:52:02

要在服务器端执行此操作,我将依赖查询字符串参数。让父页面通过附加 ?inframe=1 加载子页面。然后在 Page_Load 中检查该值。

To do it server-side, I would rely on a querystring parameter. Have the parent page load the child page by appending ?inframe=1. Then check for that value in your Page_Load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文