簡體   English   中英

如何在 iframe 中回發 aspx 頁面?

[英]How to postback aspx page in iframe?

我的主aspx頁面中有一個iframe ,這個iframe也有一個aspx頁面。 iframe 的頁面有一些控件,主 aspx 頁面上有一個提交按鈕。 現在我想使用javascript在提交按鈕的點擊事件上postback發 iframe 的頁面。 所以主頁仍然是 static 並且 iframe 的頁面用於postback
更新
謝謝@Kevin Babcock 和@Bala R
假設我是否需要通過主頁執行按鈕(在 iframe 中)單擊事件

從 javascript 你可以嘗試類似

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

在父頁面(Parent.aspx 頁面)中包含一個 iframe 頁面(FrameChild.aspx 頁面),需要時我們需要調用 iframe 按鈕事件的方法,然后我們必須像這樣跟隨。

在父頁面 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.

在子頁面(FrameChild.aspx)中應該包含一個 function。

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

假設 FrameChild.aspx 中有一個按鈕。

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

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) { }

上面的代碼對我有用。

嘗試以下操作:

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

這假設以下情況:

  • 您的頁面上沒有多個 iframe(如果有,您需要在 frames 數組中引用正確的 iframe)
  • 您的 iframe 中沒有多個 forms (如果是這樣,您需要在 forms 數組中引用正確的一個)
  • 您在 iframe 中加載的頁面是從相同的域、端口和協議加載的(否則,由於瀏覽器安全性,您可能無法訪問 iframe 內容)

向您的頁面添加一個按鈕並將其命名為 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;"
}

支持的所有瀏覽器

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

或者

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM