0

I have 2 pages, Cart.aspx and SelectPartner.aspx. And a JavaScript file, popup.js

In Cart.aspx I have a button that opens the page SelectPartner.aspx (as a new window) by the InvokePop() function.

In SelectPartner.aspx I have a gridview (with Selection enabled), a textbox, and buttons Ok and Cancel. This is what I want to do: when I select an item in the gridview, the value of one column is shown in the textbox, and when when I press the button Ok the function ReturnPartner() is called and should close the this window (SelectPartner.aspx) and show the value of this textbox in another textbox in the Cart.aspx page. If I write something into the TextBox in SelectPartner.aspx I can pass that value to the TextBox in the Cart.aspx page, but when I press a select button in the gridview the value is not passed.

I don't know what happens, please help me...

Here is the code of Cart.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Cart.aspx.cs" Inherits="NMv01.Cart" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">
        #Select1
        {
            height: 16px;
            width: 24px;
        }
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="popup.js"></script>
    <asp:Label ID="lblPartnerId" runat="server" Text="ID del Socio"></asp:Label>
                <br />
                <asp:TextBox ID="txtPartnerID" runat="server"></asp:TextBox>
                &nbsp;&nbsp;<asp:Button ID="btnPartnerId" runat="server" Text="Elegir Socio" />
                &nbsp;


</asp:Content>

Now SelectPartner.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SelectPartner.aspx.cs" Inherits="NMv01.catalog.SelectPartner" %>

<!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>
    <script type="text/javascript" src="popup.js"></script>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:GridView ID="grdSelectPartner" runat="server" AutoGenerateColumns="False" 
                        AutoGenerateSelectButton="True" DataKeyNames="PartnerId" 
                        DataSourceID="srcSelectPartner" 
                        onselectedindexchanged="grdSelectPartner_SelectedIndexChanged">
                        <Columns>
                            <asp:BoundField DataField="PartnerName" HeaderText="PartnerName" 
                                SortExpression="PartnerName" />
                            <asp:BoundField DataField="PartnerId" HeaderText="PartnerId" ReadOnly="True" 
                                SortExpression="PartnerId" />
                            <asp:BoundField DataField="PartnerCity" HeaderText="PartnerCity" 
                                SortExpression="PartnerCity" />
                        </Columns>
                    </asp:GridView>
                    <asp:SqlDataSource ID="srcSelectPartner" runat="server" 
                        ConnectionString="Data Source=ZUNIGA-PC\SQL1;Initial Catalog=NovamMonetanDB;User ID=sa; pwd=Next2011" 
                        ProviderName="System.Data.SqlClient" 

                        SelectCommand="SELECT [PartnerName], [PartnerId], [PartnerCity] FROM [Partners] ORDER BY [PartnerName]">
                    </asp:SqlDataSource>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="ID:"></asp:Label>
                    &nbsp;<asp:TextBox ID="txtPartner" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnOk" runat="server" Text="OK" OnClientClick="ReturnPartner()" />
                    &nbsp; <asp:Button ID="btnCancel" runat="server" Text="Cancelar" />
                    <br />
                    <br />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>

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

And this is the popup.js file:

function InvokePop(fname) {
    val = document.getElementById(fname).value;
    // to handle in IE 7.0          
    if (window.showModalDialog) {
        retVal = window.showModalDialog("SelectPartner.aspx?Control1=" + fname, 'Choose Partner', "dialogHeight:360px,dialogWidth:360px,resizable:yes,center:yes,");
        document.getElementById(fname).value = retVal;
    }
    // to handle in Firefox
    else {
        retVal = window.open("SelectPartner.aspx?Control1=" + fname, 'Choose Partner', 'height=360px,width=360px,resizable=yes,modal=yes');
        retVal.focus();
    }
}


function ReturnPartner() {
    var returnString = document.getElementById('txtPartner').value;
    RetrieveControl();
    // to handle in IE 7.0
    if (window.showModalDialog) {
        window.returnValue = returnString;
        window.close();
    }
    // to handle in Firefox
    else {
        if ((window.opener != null) && (!window.opener.closed)) {
            // Access the control.       
            window.opener.document.getElementById(ctr[1]).value = returnString;
        }
        window.close();
    }
}

function RetrieveControl() {
    //Retrieve the query string
    queryStr = window.location.search.substring(1);
    //Retrieve the control passed via querystring
    ctr = queryStr.split("=");
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
myrmix
  • 371
  • 3
  • 11
  • 25

1 Answers1

1

I would suggest you to remove the use of window.showModalDialog() and use window.open() instead because Window.open() is supported uniformly by all browsers while window.ShowModalDialog() is an MSIE feature only.

showModalDialog() is said to have trouble "posting back" for which a iframe hack is needed.

i tested your code with window.open for all browsers and it worked great.

Pankaj Kumar
  • 1,748
  • 6
  • 28
  • 41
  • Thank you! That was the problem. By the way, do you know how can I pass a parameter to ReturnPartner() in this context? I tried many ways without success. – myrmix Jan 09 '12 at 16:52
  • Sorry for the late reply. have a look at this link http://stackoverflow.com/q/87359/168371 – Pankaj Kumar Jan 20 '12 at 06:16