[轉貼] 利用showModalDialog來做一個父子視窗互相傳值的功能

2012040314:52

出處:http://www.dotblogs.com.tw/puma/archive/2008/06/29/4401.aspx


 

這個範例很早以前就寫好了..因為前陣看到有人在討論這個東西...

小弟去msdn找了一些範例..也實作一個小小的實例..分享給大家呀...

目前測試IE7,Firefox3都可以用...其它版本我就不知道了..有興趣再自己去測吧..

asp.net(c#)

showModelessDialogEX.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="showModelessDialogEX.aspx.cs" Inherits="Test.showModelessDialogEX" %>

<!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 id="Head1" runat="server">
    <title>利用showModalDialog來做一個父子視窗互相傳值的功能</title>
    <script type="text/javascript">
        function CallDialog() {
            window.showModalDialog("myDialog.aspx", window, "status:false;dialogWidth:300px;dialogHeight:300px;dialogLeft:50px;dialogTop:200px");
        }

        function SetTextBox(str) {
            //原本程式,請修正成下面程式
            //document.getElementById("TextBox1").value = str;

            //如果遇到有套MasterPage,上面的程式會死掉
            var id = '<%=this.TextBox1.ClientID %>';
            document.getElementById(id).value = str;
        }
    </script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onclick='CallDialog();'></asp:TextBox>
    </div>
    </form>
</body>
</html>

myDialog.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myDialog.aspx.cs" Inherits="Test.myDialog" %>

<!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 id="Head1" runat="server">
    <title>myDialog</title>
    <script type="text/javascript">
        function GetInfo(str) {
            var arg = window.dialogArguments;
            arg.SetTextBox(str);
            window.close();
        }
    </script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server">Select</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

myDialog.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Test
{
    public partial class myDialog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridView1.DataSource = new string[] { "Dotblogs", "F6 Team", "puma" };
                this.GridView1.DataBind();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lnkbtn = e.Row.Cells[0].FindControl("LinkButton1") as LinkButton;
                lnkbtn.OnClientClick = string.Format("GetInfo('{0}'); return false;", e.Row.Cells[1].Text);
            }
        }
    }
}

執行結果:

參考網址:

http://msdn.microsoft.com/en-us/library/ms536759(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms533723(VS.85).aspx