将网格视图值从弹出窗口传递回asp.net中父页面的文本框

时间:2014-02-11 10:07:26

标签: c# javascript asp.net gridview

我需要将Gridview of Pop-up窗口中的值返回到父页面的 asp:textbox 父页面代码如下所示。

  <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"  AutoEventWireup="true" CodeFile="page8.aspx.cs" Inherits="page8" %>

 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
  <script type="text/javascript">
     function OpenPopup() {
        window.open("Default5.aspx", "Popup",  "scrollbars=no,resizable=no,width=500,height=250");

    }
</script>

       <table width="80%">
                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text="Value from popup:"></asp:Label>
                       </td>
                        <td>
                            <asp:TextBox ID="txtOpenner" runat="server"></asp:TextBox>
                             <asp:Button ID="Button1" runat="server" Text="Popup" OnClientClick="OpenPopup()" />
                            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                        </td>
                    </tr>
                </table>

子窗口代码如下所示。

   child window(popup)



  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

  <!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>
  </head>
 <body>
  <form id="form1" runat="server">
 <script language="javascript" type="text/javascript">

     function SendValue(ID) {

         window.opener.document.getElementById("txtOpenner").value = ID;
        window.close();

    }

 </script>

  <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" DataSourceID="SqlDataSource2">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("DisplayName")   %>'></asp:Label>                    
                <div>
                    <a href="javascript:SendValue('<%# Eval("ID") %>');">
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("ID") %>'>   </asp:Label>    
                    </a>
                </div>
            </ItemTemplate>
        </asp:TemplateField>          
    </Columns>
   </asp:GridView>
   <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConStr %>" 
        SelectCommand="select ID,DisplayName from TreeNodes_M">
    </asp:SqlDataSource>
</form>

  </body>
  </html>

我没有在父文本框中获得子页面选择gridview的值。非常感谢。

1 个答案:

答案 0 :(得分:0)

因为使用master page 'txtOpenner' ID的父网页已更改为'ContentPlaceHolder3_txtOpenner'而您无法通过此代码获取该元素

window.opener.document.getElementById("txtOpenner").value = ID;

您应该将TextBox的ClientIDMode属性设置为static

<asp:TextBox ID="txtOpenner" ClientIDMode="Static" runat="server"></asp:TextBox>

然后你的代码:

window.opener.document.getElementById("txtOpenner").value = ID;

将起作用