javascript 如何在iframe中回发aspx页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5844504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:32:55  来源:igfitidea点击:

How to postback aspx page in iframe?

javascriptasp.nethtmliframe

提问by jams

I have an iframein my main aspxpage and this iframehas also a aspxpage. iframe's page has some controls and main aspx page has a submit button on it. Now I want to postbackiframe's page on submit button's click event using javascript. So that main page remain static and iframe's page goes for a postback
UPDATE
Thanks @Kevin Babcock and @Bala R
Suppose if I need to perform a button (which is in iframe) click event through main page

我的主页中有一个iframeaspxiframe也有一个aspx页面。iframe 的页面有一些控件,主 aspx 页面上有一个提交按钮。现在我想在postbackiframe 的页面上使用javascript. 所以主页保持静态并且 iframe 的页面进行postback
更新
谢谢@Kevin Babcock 和@Bala R
假设我需要通过主页执行一个按钮(在 iframe 中)点击事件

采纳答案by Bala R

From javascript you can try something like

从 javascript 你可以尝试类似的东西

document.frames["iframeid"].document.forms[0].submit();

回答by Udeesh

In the parent page(Parent.aspx page) contains one iframe page(FrameChild.aspx page), when you need we need to call a method of iframe button event, then we have to follow like this.

在父页面(Parent.aspx 页面)中包含一个iframe 页面(FrameChild.aspx 页面),当你需要我们需要调用一个iframe 按钮事件的方法时,我们就得像这样。

In the parent page javascript method call like this:

在父页面 javascript 方法调用是这样的:

<script type="text/javascript" language="javascript">
    function functionName()    {
        document.getElementById(iframeID).contentWindow.MyIFrameFunction(); 
    }

iframeID is the frame id, MyIFrameFunction() is the name of javascript function in the FrameChild.aspx, and call the functionName() method through the button of parent page, which inturn call the below MyIFrameFunction() of the child .js function.

iframeID是frame id,MyIFrameFunction()是FrameChild.aspx中javascript函数的名称,通过父页面的按钮调用functionName()方法,依次调用子.js函数下面的MyIFrameFunction()。

In the child page(FrameChild.aspx) should contain one function.

在子页面(FrameChild.aspx)中应该包含一个函数。

<script type="text/javascript" language="javascript">
    function MyIFrameFunction() {            
        __doPostBack('btnRenew', 'passparameter');
    }
</script>

assume one button in FrameChild.aspx.

假设 FrameChild.aspx 中有一个按钮。

<asp:Button ID="btnRenew" runat="server" OnClick="btnRenew_Click" />

code behind of FrameChild.aspx page.

FrameChild.aspx 页面背后的代码。

protected void Page_Load(object sender, System.EventArgs e)
{       
    string target = Request["__EVENTTARGET"]; //btnRenew
    string paremeter = Request["__EVENTARGUMENT"];  //passparameter
    if (!string.IsNullOrEmpty(target) && !string.IsNullOrEmpty(arg))
    {
        btnRenew_Click(sender, e);
    }
}

public void btnRenew_Click(object sender, EventArgs e) { }

The above code work for me.

上面的代码对我有用。

回答by Kevin Babcock

Try the following:

请尝试以下操作:

window.frames[0].document.forms[0].submit();

This assumes the following things:

这假设以下几点:

  • you don't have multiple iframes on your page (if so, you need to reference the correct one in the frames array)
  • you don't have multiple forms in your iframe (if so, you need to reference the correct one in the forms array)
  • the page you have loaded in the iframe is loaded from the same domain, port, and protocol (otherwise, you may not be able to access the iframe content due to browser security)
  • 您的页面上没有多个 iframe(如果有,您需要在框架数组中引用正确的一个)
  • 您的 iframe 中没有多个表单(如果有,您需要在表单数组中引用正确的表单)
  • 您在iframe中加载的页面是从相同的域、端口和协议加载的(否则,由于浏览器安全,您可能无法访问iframe内容)

回答by MikeTeeVee

Add a button to your page and call it something like ID="btn_ParentSubmit".
Let's say you named your iframe ID="iframeTest"
In your Page_Load()add this:

在您的页面中添加一个按钮并将其命名为 ID=" btn_ParentSubmit"。
假设您命名 iframe ID=" iframeTest"
在您的Page_Load() 中添加:

if (IsPostBack == false)
{
    //Either place the button in an UpdatePanel (so it won't reload the iFrame) -OR- add "return false;" to prevent the post-back and let the iFrame submit itself. - 09/25/2012 - MCR.
    btn_ParentSubmit.OnClientClick = "javascript:document.getElementById('" + iframeTest.ClientID + "').contentDocument.forms[0].submit(); return false;"
}

回答by Heera

Every Browser Supported

支持的每个浏览器

 window.frames[0].document.getElementById('btnSave').click();  

or

或者

 window.frames['iframename'].document.getElementById('btnSave').click();