通过TabPanel在Parent中传递子值

本文关键字:TabPanel Parent 通过 | 更新日期: 2023-09-27 18:12:03

我有问题的脚本传递值从孩子到父母与tabpanel(控制器)。在这个选项卡中有一个文本框。当我试图传递TabPanel内的值时,它不起作用。当我移开面板时,它可以工作。

这里的html脚本来自

Parent.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
  <cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
            <cc1:TabPanel runat="server" HeaderText="TabPanel1" ID="TabPanel1">
                <HeaderTemplate>
                    TabPanel1<asp:ScriptManager ID="ScriptManager1" runat="server">
                    </asp:ScriptManager>
                </HeaderTemplate>
                <ContentTemplate>
                     <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="button1" runat="server" OnClick="button1_Click" Text="Button" />
    </div>
                </ContentTemplate>
            </cc1:TabPanel>
            <cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="TabPanel2">
            </cc1:TabPanel>
        </cc1:TabContainer>
    </form>
</body>
</html>

这是子元素和Javascript

child.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Child.aspx.cs" Inherits="Child" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="Initialtest" runat="server"></asp:TextBox>
    <br />
        <asp:TextBox ID="Initialtest2" runat="server"></asp:TextBox>
        <br />
       <%--<input type="button" value="Select" onclick="SetName();" />--%>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
         <script type="text/javascript">
             function SetName() {
                 if (window.opener != null && !window.opener.closed) {
                     var txtName = window.opener.document.getElementById("TextBox1");
                     txtName.value = document.getElementById("Initialtest").value;
                     var txtName1 = window.opener.document.getElementById("TextBox2");
                     txtName1.value = document.getElementById("Initialtest2").value;
                 }
                 alert("test");
                 window.close();
             }
</script>

    </form>
</body>
</html>

child.aspx.cs(后面的代码)

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

    }


//protected void Button1_Click(object sender, EventArgs e)
//{
//   //
//   Button1.Attributes.Add("onclick", "javascript:SetName();");
//}
    protected void Button1_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "SetName()", true);
    }
}

子场景必须将值传递给TabPanel中的textbox1。希望有人能帮我解决这个问题。

通过TabPanel在Parent中传递子值

好了,我解决了这个问题。

<script type="text/javascript">
    function SetName() {
        if (window.opener != null && !window.opener.closed) {
            var txtName = window.opener.document.getElementById("TextBox1");
            txtName.value = document.getElementById("Initialtest").value;
            var txtName1 = window.opener.document.getElementById("TextBox2");
            txtName1.value = document.getElementById("Initialtest2").value;
        }
            alert("test");
            window.close();
    }
</script>