Search code examples
javascriptc#asp.netjavascript-objectsdom-events

Passing child values in Parent with TabPanel


I am having problem with the script of passing values from child to parent with tabpanel (controller). Within that Tab there's a textbox. When I tried to pass a value that within the TabPanel it doesn't work. When I removed the panel it works.

Here the html script from

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>

and here's the child and 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>

the child.aspx.cs (code behind)

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

The scenario child must pass the value to textbox1 within that TabPanel. Hoping that someone will help me to resolve the problem.


Solution

  • Done, I resolved this problem.

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