JavaScript实现弹出子窗口并传值给父窗口
新建父窗口页面:
加入以下脚本
<htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <title>First</title> </head> <scripttype="text/javascript"> functionShowDialog(Url) { var iWidth=560;//模态窗口宽度 var iHeight=300;//模态窗口高度 var iTop=(window.screen.height-iHeight-100)/2; var iLeft=(window.screen.width-iWidth)/2; varreturnValue=window.showModalDialog(Url,"newwindow","dialogHeight:"+iHeight+"px;dialogWidth:"+iWidth+"px;toolbar:no;menubar:no;scrollbars:no;resizable:no;location:no;status:no;left:200px;top:100px;"); document.getElementById("TextBox1").innerText=returnValue; } </script> <body> <formid="form1"runat="server"> <inputid="Button1"type="button"value="button"onclick="ShowDialog('./second.aspx')"/> <div><asp:TextBoxID="TextBox1"runat="server"Width="67px"></asp:TextBox></div> </form> </body> </html>
创建second.html页面:
<htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <title>second</title> <!--<basetarget="_self"></base>--> </head> <scriptlanguage="javascript"> functioncloseDiag() { window.opener=null; window.close(); } functionOK() { varValue=document.getElementsByName("myRadio"); for(vari=0;i<Value.length;i++) { if(Value[i].checked) { window.returnValue=Value[i].value; window.close(); } else { document.getElementById("Lab_Info").innerText="没有选中项,请选择!"; } } } </script> <body> <formid="form1"runat="server"> <div> <asp:DataGridID="dgSjygl"runat="server"AllowPaging="True"AutoGenerateColumns="False" BackColor="White"Font-Size="9pt"PageSize="5"Width="100%"Height="127px"OnItemDataBound="dgSjygl_ItemDataBound"> <PagerStyleMode="NumericPages"/> <AlternatingItemStyleBackColor="Gainsboro"/> <ItemStyleBackColor="#EEEEEE"ForeColor="Black"HorizontalAlign="Center"/> <Columns> <asp:TemplateColumn> <HeaderTemplate> <label>选择</label> </HeaderTemplate> <ItemTemplate> <inputtype="radio"id="myRadio"name="myRadio" value='<%#DataBinder.Eval(Container.DataItem,"id")%>'> </ItemTemplate> <HeaderStyleFont-Bold="True"HorizontalAlign="Center"Width="60px"/> <ItemStyleHorizontalAlign="Center"/> </asp:TemplateColumn> <asp:BoundColumnDataField="ID"HeaderText="编号"> <HeaderStyleFont-Bold="True" Width="75px"HorizontalAlign="Center"/> </asp:BoundColumn> <asp:BoundColumnDataField="MC"HeaderText="名称"> <HeaderStyleFont-Bold="True" Width="90px"HorizontalAlign="Center"/> <ItemStyleHorizontalAlign="Center"/> </asp:BoundColumn> <asp:BoundColumnDataField="BT"HeaderText="标题"> <HeaderStyleFont-Bold="True"HorizontalAlign="Center"/> </asp:BoundColumn> </Columns> <HeaderStyleBackColor="#7481BA"ForeColor="Yellow"Font-Bold="False"Font-Italic="False"Font-Overline="False"Font-Strikeout="False"Font-Underline="False"Font-Size="Small"/> </asp:DataGrid> </div> <div> <inputid="Btn_OK"type="button"onclick="OK()"value="确定"/> <inputid="Btn_Cancel"type="button"onclick="clickDiag()" value="取消"/> </div> </form> </body> </html>
以上就是本人实现这个简单功能的全部内容了,感觉应该还有更便捷的方法,有知道的小伙伴还请留言说明下。