简体   繁体   English

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

[英]Passing grid view value from a popup window back to the textbox of parent page in asp.net

I need to return the value from Gridview of Pop-up window to asp:textbox of parent Page Code of Parent Page is shown below. 我需要将弹出窗口的Gridview的值返回给asp:父页面的父页面的文本框 ,如下所示。

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

Code of child window is specified below. 子窗口的代码在下面指定。

   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>

I am not getting the value of child page selected gridview in the textbox of parent.Any help are appreciated. 我没有在父级的文本框中获得选择的子页面gridview的值。不胜感激。

because your parent page using master page the id of 'txtOpenner' is changed to 'ContentPlaceHolder3_txtOpenner' and you can't get the element by this code 因为您使用master page的父页'txtOpenner'更改为'ContentPlaceHolder3_txtOpenner' ,因此您无法通过此代码获取元素

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

you should set ClientIDMode property of your TextBox to static , 您应该将TextBox的ClientIDMode属性设置为static

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

and then your code : 然后你的代码:

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

will work 将工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM