简体   繁体   中英

How to postback aspx page in iframe?

I have an iframe in my main aspx page and this iframe has also a aspx page. iframe's page has some controls and main aspx page has a submit button on it. Now I want to postback iframe'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

From javascript you can try something like

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

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.

In the parent page javascript method call like this:

<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.

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

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

assume one button in FrameChild.aspx.

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

code behind of FrameChild.aspx page.

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.

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)

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:

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

Every Browser Supported

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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