程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net彈出窗口 返回值

asp.net彈出窗口 返回值

編輯: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中html的window對象的屬性,目的是返回窗口值,當用
window.showModalDialog函數打開一個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>

復制代碼 代碼如下:
//child.html
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="javascript">
function trans(tag){

   if (tag==0){
     window.returnValue=false;
   } else{
     window.returnValue =true;
   }
   window.close();

}


</script>
</HEAD>
<BODY>

<INPUT id=button1 type=button value="返回真" name=button1 onclick="trans(1)">
<INPUT id=button2 type=button value="返回假" name=button2 onclick="trans(0)">

</BODY>
</HTML>

這樣一來可以實現從模式窗口向父窗口傳遞值的作用,
這個returnValue除了可以是布爾值,整型值等以外還可以是個js數組,用來傳遞大量數據。
具體showModalDialog等的用法,可以參考msdn。

 

注意下面的有opener的都只能是用在window.open()這種情況而不能是上面.的showModel...等形式否則的話.會報undetife錯誤....


也可以這樣子的改變父窗口中的值. 下面的這個..可以動態改變父窗口中多個值.而不是簡單的把彈出窗口中的一個選中以後.馬上就傳回去給父窗口.


opener.document.getElementById('txt_Phone').value = Number;
        opener.document.getElementById('hdn_ID').value = ID;
        opener.document.getElementById('hdn_Phone').value = Number;
        window.close();


加上這句.我們還可以.刷新父窗口
window.opener.location.href=window.opener.location.href
window.opener.location.reload()

如果還要調用父窗口中的方法.也可以用下面的這種..如下
     opener.函數名(xxx,xxx)  
不過函數內變量的作用域仍為父窗體.
這樣子我們.就可以直接調用這個函數..如果這個函數是異步請求的那就更爽了..
也就是說我們.在子窗口中可以向服務器發送請求..關閉子窗口後..我們父窗口又立即向服務器發送異步請求.又窗口雙請求.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved