简体   繁体   English

从父aspx页面调用子iframe方法

[英]Call child iframe method from Parent aspx page

I've created one aspx page Default.aspx. 我创建了一个aspx页面Default.aspx。 It contains iframe with child.aspx. 它包含带有child.aspx的iframe。 File Child.cs contains web methods which should be called by parent page. 文件Child.cs包含应由父页面调用的Web方法。

As I understand I need to receive the instance of Child class so then I'll be able to them call. 据我了解,我需要接收Child类的实例,这样我就可以调用它们。 How can I make this? 我该怎么做?

I will be really appreciated for constructive solution, 对于建设性的解决方案,我将非常感激,

Default.aspx (Parent page) Default.aspx(父页面)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Parent_Child_Iframe._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Button ID="btGetDataFromChild" runat="server" Text="Get Data from Child" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <iframe id="I1" height="300px" name="I1" src="child.aspx" width="400px"></iframe>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Child.aspx Child.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="Parent_Child_Iframe.Child" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Child.aspx.cs Child.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Parent_Child_Iframe
{
    public partial class Child : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public string getResultData ()
        {
            return TextBox1.Text + TextBox2.Text;
        }
    }
}

So How I can call getResultData () from Child.aspx.cs by clicking Button on the parent page. 因此,如何通过单击父页面上的Button从Child.aspx.cs调用getResultData()。 The main difficulty is that I can't use Javascript to collect the information from TextBoxes because the task is more complicated and I can use only server methods. 主要的困难是我不能使用Javascript从TextBoxes收集信息,因为任务更加复杂并且只能使用服务器方法。

Thank you in advance, Best wishes, Greg. 预先感谢您,格雷格。

Thats not really the way iframes work. 那不是iframe运作的方式。 Remember, HTTP is a stateless protocol, which means that each request for a page is an independent request, and won't have any context of the previous request. 请记住,HTTP是一个无状态协议,这意味着对页面的每个请求都是一个独立的请求,并且不会包含先前请求的任何上下文。 What I think you would need to do, is either request the Child.aspx page with specific form or request variables, and the Child page should output the correct page accordingly. 我认为您需要做的是请求具有特定形式的Child.aspx页面或请求变量,并且Child页面应相应地输出正确的页面。

Could you explain what it is you want to achieve? 您能解释一下您要实现的目标吗?

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

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