在gridview中选择项目后,Javascript函数无法获取文本框值(文本)

时间:2012-01-09 06:03:38

标签: javascript asp.net

我有2页,Cart.aspx和SelectPartner.aspx。还有一个JavaScript文件popup.js

在Cart.aspx中,我有一个按钮,可以通过InvokePop()函数打开SelectPartner.aspx页面(作为一个新窗口)。

在SelectPartner.aspx中,我有一个gridview(启用了Selection),一个文本框,以及Ok和Cancel按钮。这就是我想要做的事情:当我在gridview中选择一个项目时,一列的值显示在文本框中,当我按下按钮时,调用函数ReturnPartner()并关闭此窗口( SelectPartner.aspx)并在Cart.aspx页面的另一个文本框中显示此文本框的值。如果我在SelectPartner.aspx的TextBox中写了一些内容,我可以将该值传递给Cart.aspx页面中的TextBox,但是当我按下gridview中的select按钮时,该值不会传递。

我不知道会发生什么,请帮助我......

以下是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>

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

这是popup.js文件:

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("=");
}

1 个答案:

答案 0 :(得分:1)

我建议您删除window.showModalDialog()的使用并使用window.open(),因为所有浏览器都统一支持Window.open(),而window.ShowModalDialog()仅是MSIE功能。

showModalDialog()据说有问题&#34;回帖&#34;需要iframe黑客。

我使用window.open为所有浏览器测试了你的代码并且效果很好。