GridView子窗口向父窗口传值问题?[

本文介绍了不同网页间如何有效传递参数的方法,包括父窗体向子窗体传值、子窗体向父窗体传值及多级网页间的传值等场景,并对比了window.open()与window.showModalDialog()两种弹窗方式的传值区别。

一.子窗体向父窗体传值

1、父窗体:
<script type="text/javascript">
       
function XuanZeContract()
        {
            window.showModalDialog(
'子窗体.aspx','newwindow','height=300,width=500,top='+(screen.AvailHeight-300)/2+',left='+(screen.AvailWidth-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
        }

父窗体后台取值:

TextBox1.Text = Request.QueryString["id"];

HTML code:

 <input id="Button3" type="button" value="选择" onclick="XuanZeContract()" />

2、子窗体后台双击选择:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
//鼠标双击事件
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add(
"onDblClick", "javascript:opener.location.href='父窗体.aspx?id=" + e.Row.Cells[1].Text.ToString() + "';window.close();");
        }
    }

二、现在又是父窗体向子窗体传值

 1、父窗体:

 <ItemTemplate>
                       
<asp:LinkButton ID="LinkButton5" runat="server" ForeColor="#000066">查看</asp:LinkButton>
                       
<asp:Label ID="Label2" runat="server"
                            Text
='<%#DataBinder.Eval(Container.DataItem, "F_name")%>' Visible="False" Width="0px"></asp:Label>
                   
</ItemTemplate>

C# code

if (e.Row.RowType == DataControlRowType.DataRow)
  {
  LinkButton l = (LinkButton)e.Row.FindControl("LinkButton5");
  Label b = (Label)e.Row.FindControl("Label2");
  string Temp = b.Text.ToString();
  l.Attributes.Add("onclick", "window.showModalDialog('ContractParticularData.aspx?id="+
Server.UrlEncode(Temp)+"','window','dialogWidth:880px;DialogHeight=700px;status:no;help:no;resizable:yes;');window.location='#';");

  }

 2、子窗体:

 

HTML code  <tr>
               
<td class="style3">客户名称:</td>
               
<td class="style1">
                   
<asp:Label ID="Label3" runat="server" Text="Label3"></asp:Label>
               
</td>
           
</tr>
C# code

 string IDStr = Request.QueryString["id"].ToString();
OleDbConnection sqlConn
= DB.CreateConn();
        sqlConn.Open();
       
string sqlStr = "select * from XXXX where F_name='" + IDStr.ToString() + "'";

        OleDbCommand MyCommand
= new OleDbCommand(sqlStr, sqlConn);
        OleDbDataReader MyReader
= MyCommand.ExecuteReader();
       
while(MyReader.Read())
        {
           
this.Label3.Text = MyReader["XXX"].ToString();
        }
        sqlConn.Close();


三、

现在的问题是,父窗体A打开子窗体B,然后子窗体B在打开窗体C(那窗体C就算是窗体B的子窗体了),请问子窗体C该如何向窗体B传值?
1、父窗体A:

this.Button1.Attributes.Add("onclick", "window.showModalDialog('子窗体B.aspx','window','dialogWidth:880px;DialogHeight=600px;status:no;help:no;resizable:yes;');window.location='#';");

2、子窗体B:

<script type="text/javascript">
       
function XuanZe()
        {
           window.open(
'孙子窗体C.aspx','newwindow','height=300,width=500,top='+(screen.AvailHeight-300)/2+',left='+(screen.AvailWidth-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
        }
</script>
<asp:TextBox ID="TextBox3" runat="server" Width="200px"></asp:TextBox>
<input id="Button3" type="button" value="选择" onclick="XuanZe()"/>
C# code

TextBox3.Text = Request.QueryString["id"].ToString();
3、孙子窗体C:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
//鼠标双击事件
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           e.Row.Attributes.Add("onDblClick", "javascript:window.opener.location.href='子窗体B.aspx?id="+e.Row.Cells[1].Text.ToString() + "';window.close();");


e.Row.Attributes.Add("onDblClick", "javascript:window.opener.documentById('txt').value='"+e.Row.Cells[1].Text.ToString() + "';window.close();");

 
        }
    }

四、用window.open(),window.showModalDialog()打开页面的区别应用

  a.用window.open()打开子窗口的传参方法

 

  a.html

HTML code
<html> <body> 要传的值 <input type='text' id='txtID' name='txtName' value='aa' /> <br> <input type='button' value='open' onclick="window.open('b.html');" /> <script> alert('刷新了页面'); function method() { alert('a.html'); } </script> </body> </html>


b.html

HTML code
<html> <script> function getValue() { //document.getElementById('txt').value=window.opener.txtName.value; document.getElementById('txt').value=window.opener.document.getElementById('txtID').value; } </script> <body onload='getValue();'> 传过来的值是 <input type='text' id='txt' /> <input type='button' value='调用父窗口的方法' onclick='window.opener.method();' /> <br> <br> <br> 设置父窗口的文本<input type='text' id='t' /> <input type='button' value='执行' onclick='window.opener.document.getElementById("txtID").value=document.getElementById("t").value;' /> <br> <br> <input type='button' value='刷新父窗口' onclick='window.opener.location=window.opener.location;' /> <br> <br> <input type='button' value='关闭父窗口' onclick='window.opener.close();opener=null;' /> </body> </html> b,用window.showModalDialog()打开窗口的传参方法
c.html
HTML code
<html> <script> function getValue() { if(window.dialogArguments) { document.getElementById('txt').value=window.dialogArguments.document.getElementById('txtID').value; } } </script> <body onload='getValue();'> 传过来的值是 <input type='text' id='txt' /> <input type='button' value='调用父窗口的方法' onclick='window.dialogArguments.method();' /> <br> <br> <br> 设置父窗口的文本<input type='text' id='t' /> <input type='button' value='执行' onclick='window.dialogArguments.document.getElementById("txtID").value=document.getElementById("t").value;' /> <br> <br> <input type='button' value='刷新父窗口' onclick='window.dialogArguments.location=window.dialogArguments.location;' /> <br> <br> <input type='button' value='关闭父窗口' onclick='window.dialogArguments.close();' /> </body> </html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值