【问题标题】:How to postback aspx page in iframe?如何在 iframe 中回发 aspx 页面?
【发布时间】:2011-08-16 05:09:38
【问题描述】:

我的主要aspx 页面中有一个iframe,这个iframe 也有一个aspx 页面。 iframe 的页面有一些控件,主 aspx 页面上有一个提交按钮。现在我想postback iframe 的页面使用javascript 提交按钮的点击事件。因此主页保持静态,iframe 的页面为postback
更新
谢谢@Kevin Babcock 和@Bala R
假设我是否需要通过主页执行按钮(在 iframe 中)单击事件

【问题讨论】:

    标签: javascript asp.net html iframe


    【解决方案1】:

    支持所有浏览器

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

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

    【讨论】:

      【解决方案2】:

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

      在父页面的javascript方法调用如下:

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

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

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

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

      上面的代码对我有用。

      【讨论】:

        【解决方案3】:

        向您的页面添加一个按钮并将其命名为 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;"
        }
        

        【讨论】:

          【解决方案4】:

          尝试以下方法:

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

          这假设了以下几点:

          • 您的页面上没有多个 iframe(如果有,您需要在 frames 数组中引用正确的 iframe)
          • 您的 iframe 中没有多个表单(如果有,您需要在表单数组中引用正确的表单)
          • 您在 iframe 中加载的页面是从相同的域、端口和协议加载的(否则,由于浏览器安全性,您可能无法访问 iframe 内容)

          【讨论】:

            【解决方案5】:

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

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

            【讨论】:

            • document.frames 不适用于所有浏览器(例如 Chrome)。请改用 windows.frames。
            • @jams 我相信你可以做到document.frames["iframeid"].document.forms[0].getElementById('buttonId').click();
            • @Bala R:它给出了错误Microsoft JScript runtime error: Object doesn't support this property or method
            • @Bala R:我自己解决了document.frames["iframeid"].document.getElementById('buttonId').click(­);
            • 无论如何感谢您的帮助:)
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-06-01
            • 2013-01-18
            • 1970-01-01
            • 2011-09-17
            • 2011-01-10
            • 2013-02-10
            • 1970-01-01
            相关资源
            最近更新 更多