asp.netポップアップウィンドウの戻り値


Page.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> </title>
<script type="text/javascript" >...
function Pop()
...{
var result=showModalDialog('downs.aspx','subpage','dialogWidth:400px;dialogHeight:300px;center:yes;help:no;resizable:no;status:no'); // ,
document.getElementById("txt_id").value=result.split("'")[0]; //
document.getElementById("txt_name").value=result.split("'")[1];
document.getElementById("txt_pwd").value=result.split("'")[2];
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt_id" runat="server" ></asp:TextBox>
<asp:TextBox ID="txt_name" runat="server" ></asp:TextBox>
<asp:TextBox ID="txt_pwd" runat="server" ></asp:TextBox>
<br />

<asp:Button ID="btnPop" runat="server" Text="PoPWindows" />

</div>
</form>
</body>
</html>

downs.aspx:ポップアップページ

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> </title>
<script type="text/javascript" >...
function cc(infor_id,infor_name,infor_psw) // id,name password
...{
window.returnValue= infor_id+"'"+infor_name+"'"+infor_psw; //
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvshow" runat="server" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" Horiz />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
downs.cs:ポップアップページのバックグラウンドコード:

public partial class downs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetBind();
}
}
public void SetBind()
{
string ConnString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
string sql = "select top 10 gwid,machtype,isok from allinfor";
SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
ada.Fill(ds);
gvshow.DataSource = ds.Tables[0];
this.gvshow.DataBind();
}
}
protected void gvshow_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "cc('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text + "','" + e.Row.Cells[2].Text + "')");
}
}
}
第二の方法:
returnValueはjavascriptのwindowオブジェクトの属性であり、ウィンドウ値を返すことを目的として、window.show ModalDialog関数でIEのモードウィンドウを開くと(モードウィンドウを開くと親ウィンドウが操作できなくなり、モードウィンドウが閉まるまで待つしかない)、ウィンドウの値に戻ります。

//father.html
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="javascript">

function showmodal(){
var ret = window.showModalDialog("child.htm",null,"dialogWidth:350px;dialogHeight:350px;help:no;status:no");
if (ret){alert(' !');
}else{
alert(' !');
}

}

</script>
</HEAD>
<BODY>
<INPUT id=button1 type=button value=Button name=button1 onclick="showmodal();">

</BODY>
</HTML>

これにより、モードウィンドウから親ウィンドウに値を転送する役割を果たすことができ、このreturn Valueは、ブール値、整数値などの他に、多くのデータを伝えるためのjs配列とすることができる。具体的にshow ModalDialogなどの使い方は、msdnを参照することができます。
 
次のopenerがあるのはwindow.openだけです。このような状況では上ではなく、show Model...などの形式でなければ、undetifeエラーを報告します。
このように親ウィンドウの値を変更することもできます。これは、親ウィンドウの複数の値を動的に変えることができます。簡単にポップアップウィンドウの中の一つを選択したのではなく、すぐに親ウィンドウに戻します。
opener.document.getElemenntById('txt_Phone'.value=Number;        opener.document.getElemenntById('hdn_ID').value=ID        opener.document.getElemenntById('hdn_Phone'.value=Number;        window.close()
これを追加します。私たちは父の窓口window.opener.locations.href=window.opener.locations.href window.opener.locations.reloadを更新します。
また、親ウィンドウのメソッドを呼び出す場合は、以下のようにすることもできます。     opener.関数名(xxx,xxx)   しかし、関数内の変数のスコープはまだ親のフォームです。このように私たちは直接にこの関数を呼び出すことができます。この関数が非同期の要求であれば、より快適です。つまり、私たちはサブウィンドウでサーバに要求を送ることができます。サブウィンドウを閉じると、私たちの父のウィンドウはすぐにサーバに非同期の要求を送信します。また、ウィンドウのダブル要求もします。