Pass value from Popup window to main window using JavaScript in ASP.Net

Last Reply on Apr 27, 2016 03:04 AM By dharmendr

Posted on Apr 20, 2016 01:41 AM

Morning Mr.Mudassar.

I have used your code " Pass value from child popup window to parent page window using JavaScript"

I can open popup windows and it populate data into gridview. it is working fine. But I cannot pass the value to main windows  which i have select from gridview. I capture the value from gridview into textbox and also I have a button to pass value to Main Window.

I am using Master page.

Pls Help me.

Thank you in advance



Main Window.

<script type="text/javascript">
    var popup;
    function SelectItemDESC() {
        popup ="ProductSearch.aspx", "Popup", "width=800,height=400");

Popup Windows

        <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Select Item Name"></asp:Label>
        <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Search" />
        <asp:TextBox ID="txtItemName" runat="server"></asp:TextBox>
        <asp:Button ID="btnSelectItem" runat="server" Text="select Item"  OnClientClick ="SetName();" />

        <script type="text/javascript">
            function SetName() {
                if (window.opener != null && !window.opener.closed) {
                    var txtItemID = window.opener.document.getElementById("txtItemID");
                    txtItemID.value = document.getElementById("txtItemName").value;

    <br />
     <asp:GridView ID="GridView1" runat="server" CssClass="gridview" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" 
        BorderWidth="1px" CellPadding="3" GridLines="Horizontal" PageSize="15" Font-Names="Calibri" Font-Size="Small">
        <AlternatingRowStyle BackColor="#F7F7F7" />
            <asp:CommandField ShowSelectButton="True">
            <ItemStyle Width="50px" />
            <asp:BoundField DataField="id" InsertVisible="False" ReadOnly="True" SortExpression="id" Visible="true" HeaderText="ID"/>
            <asp:BoundField DataField="itemid" HeaderText="Item Code"/>
            <asp:BoundField DataField="itemdes" HeaderText="Description"/>
            <asp:BoundField DataField="Category" HeaderText="Category"/>
            <asp:BoundField DataField="uom" HeaderText="UOM"/>
            <asp:BoundField DataField="Suppprice" HeaderText="SuppPrice"/>
            <asp:BoundField DataField="taxcode" HeaderText="Tax Code"/>

        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
         <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <SortedAscendingCellStyle BackColor="#F4F4FD" />
        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
        <SortedDescendingCellStyle BackColor="#D8D8F0" />
        <SortedDescendingHeaderStyle BackColor="#3E3277" />


Posted on Apr 27, 2016 03:04 AM

Hi maideen,

Change the function like below. What you are doing wrong is you are trying to assign tagName as span but the tagName is input and assign the value to the textbox with .value not with .innerHTML

function SelectName() {
    if (window.opener != null && !window.opener.closed) {
        var form = window.opener.document.getElementsByTagName("form")[0];
        var txtName = GetElement(form, "input", "txtItemID");
        txtName.value = document.getElementById("txtName").value;
function GetElement(parent, tagName, id) {
    var elem = parent.getElementsByTagName(tagName);
    for (var i = 0; i < parent.getElementsByTagName(tagName).length; i++) {
        if (elem[i].id.indexOf(id) != -1) {
            return elem[i];
    return null;