在asp.net中的服务器端page_load事件中获取JS返回值
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行:
应该是:(小写
value
)此外,您无法在服务器端的当前执行上下文中检查由 javascript 注册回调设置的隐藏字段的值。
我会将逻辑移至客户端以隐藏或显示复选框。如果确实必须从页面中删除该字段,您也可以使用 javascript 来完成此操作。
您可能想用 div 包裹复选框并隐藏容器以包含标签。
This line:
Should be: (lower case
value
)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.
You may want to wrap the checkbox with a div and hide the container also to include the label.
要在服务器端执行此操作,我将依赖查询字符串参数。让父页面通过附加
?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 yourPage_Load
.