【问题标题】:Passing grid view value from a popup window back to the textbox of parent page in asp.net将网格视图值从弹出窗口传递回asp.net中父页面的文本框
【发布时间】:2014-02-11 10:07:25
【问题描述】:

我需要将弹出窗口的Gridview的值返回到父Page的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>

我没有在父的文本框中获得子页面选择的网格视图的值。感谢任何帮助。

【问题讨论】:

    标签: c# javascript asp.net gridview


    【解决方案1】:

    因为您的父页面使用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;
    

    会起作用

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多