简体   繁体   English

从子页面弹出窗口访问父页面上控件的最佳方法?

[英]Best way to access controls on parent page from a child page popup?

I have a parent ASP.NET page which has a link that opens a child ASPX page in the form of a popup. 我有一个ASP.NET父页面,该页面具有一个链接,该链接以弹出形式打开一个子ASPX页面。

I need to control the parent page from the popup page's code-behind. 我需要从弹出页面的代码隐藏中控制父页面。 For example enablie/disable the visibility of a label in the parent page, or setting text value for a text box. 例如,启用/禁用父页面中标签的可见性,或设置文本框的文本值。

How can I accomplish this? 我该怎么做?

Code used to open the popup ASPX: 用于打开弹出式ASPX的代码:

//JavaScript function:
function showPopup()
{
     var strReturn = window.open("TaxReportInputsForm.aspx",'popup','width=390,height=120');
}

Button code: 按钮代码:

Page.ClientScript.RegisterStartupScript(GetType(), "popup", "showPopup();", true);

The problem that you have is that HTTP is stateless and so each HTTP request/page is handled entirely independently - this means that there is no way to directly modify what is rendered on the parent page while processing a request from the child page. 您遇到的问题是HTTP是无状态的,因此每个HTTP请求/页面都完全独立处理-这意味着在处理子页面的请求时,无法直接修改在父页面上呈现的内容。

What you can do however is indirectly modify what is rendered by having the child write out some flags/information to some place that both the child and the parent page can access, and then having the parent page look at that information to determine what to render. 但是,您可以做的是通过让孩子将一些标志/信息写到孩子和父页面都可以访问的某个地方,然后让父页面查看该信息以确定要渲染的内容,来间接地修改渲染的内容。 。 There are loads of different places that you could store this state, eg 您可以在不同的位置存储许多状态,例如

For example, your child page might write a ShowSomeLabel flag to a cookie in the code-behind, then when the child page returns to the client it forces a refresh of the parent page, then in the code-behind for the parent page it can read that flag from the cookie to determine what labels should be visible. 例如,您的子页面可能将ShowSomeLabel标志写入代码隐藏区中的cookie,然后当子页面返回客户端时,它会强制刷新父页面,然后在代码隐藏区中隐藏父页面从Cookie中读取该标志,以确定应该看到哪些标签。

All of these methods will require at least some JavaScript to cause of refresh of the parent page, however some will need additional JavaScript to copy hidden field / query string values from the child page to the parent page before it submits. 所有这些方法都将至少需要一些 JavaScript才能刷新父页面,但是有些方法还需要其他JavaScript才能在提交子页面之前将隐藏的字段/查询字符串值从子页面复制到父页面。 Exactly where and how you should store this information depends on your specific requirements. 确切的位置和存储方式取决于您的特定要求。

you can try to use javascript, suppose A.aspx is parent page and B.aspx is the open page, you can use "opener" to contronl DOM of A.aspx. 您可以尝试使用javascript,假设A.aspx是父页面而B.aspx是打开页面,则可以使用“打开器”控制A.aspx的DOM。

A.aspx link tag and javascript : A.aspx链接标记和javascript:

<a href="B.aspx" target="_blank">open page B</a>
<script type="text/javascript">
    function fun_A() {
        alert("hello");
    }
</script>

B.aspx B.aspx

<input id="btnChild" type="button" value="call function in Page A" onclick="opener.fun_A();" />

when you open B.aspx from A.aspx, and click the button from B.aspx, you will see A.aspx alert message "hello". 当您从A.aspx中打开B.aspx并单击B.aspx中的按钮时,您将看到A.aspx警报消息“你好”。

hope this can help you. 希望这可以帮到你。

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

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