ASP.NET利用showModalDialog來做一個父子視窗互相傳值的功能

来源:互联网 发布:苹果手机赌博软件 编辑:程序博客网 时间:2024/06/03 16:44
這個範例很早以前就寫好了..因為前陣看到有人在討論這個東西...

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

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

asp.net(c#)

showModelessDialogEX.aspx

view source
print?
01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showModelessDialogEX.aspx.cs"
02    Inherits="showModelessDialogEX" %>
03 
04<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05<htmlxmlns="http://www.w3.org/1999/xhtml">
06<headid="Head1"runat="server">
07    <title>showModelessDialogEX</title>
08</head>
09<body>
10    <formid="form1"runat="server">
11    <div>
12        <asp:TextBoxID="TextBox1"runat="server"onclick='CallDialog();'></asp:TextBox>
13    </div>
14    </form>
15</body>
16</html>
17 
18<scripttype="text/javascript">
19function CallDialog()                                            
20{  
21    window.showModalDialog("myDialog.aspx",window,"status:false;dialogWidth:300px;dialogHeight:300px;dialogLeft:50px;dialogTop:200px");
22}
23 
24function SetTextBox(str)
25{
26    //原本程式,請修正成下面程式
27    //document.getElementById("TextBox1").value = str;
28     
29    //如果遇到有套MasterPage,上面的程式會死掉,感謝Allen大大的提醒
30    var id = '<%=this.TextBox1.ClientID %>';
31    document.getElementById(id).value = str;
32}
33</script>

showModelessDialogEX.aspx.cs

view source
print?
01using System;
02using System.Collections;
03using System.Configuration;
04using System.Data;
05using System.Web;
06using System.Web.Security;
07using System.Web.UI;
08using System.Web.UI.HtmlControls;
09using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11 
12public partial class showModelessDialogEX : System.Web.UI.Page
13{
14    protectedvoid Page_Load(objectsender, EventArgs e)
15    {
16 
17    }
18}

myDialog.aspx

view source
print?
01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myDialog.aspx.cs" Inherits="myDialog" %>
02 
03<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04<htmlxmlns="http://www.w3.org/1999/xhtml">
05<headrunat="server">
06    <title>myDialog</title>
07</head>
08<body>
09    <formid="form1"runat="server">
10    <div>
11        <asp:GridViewID="GridView1"runat="server"
12            onrowdatabound="GridView1_RowDataBound">
13            <Columns>
14                <asp:TemplateFieldHeaderText="Select">
15                    <ItemTemplate>
16                        <asp:LinkButtonID="LinkButton1"runat="server">Select</asp:LinkButton>
17                    </ItemTemplate>
18                </asp:TemplateField>
19            </Columns>
20        </asp:GridView>
21    </div>
22    </form>
23</body>
24</html>
25 
26<scripttype="text/javascript">
27function GetInfo(str)
28{
29    var arg = window.dialogArguments;
30    arg.SetTextBox(str);
31    window.close(); 
32}
33</script>

myDialog.aspx.cs

view source
print?
01using System;
02using System.Collections;
03using System.Configuration;
04using System.Data;
05using System.Web;
06using System.Web.Security;
07using System.Web.UI;
08using System.Web.UI.HtmlControls;
09using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11 
12public partial class myDialog : System.Web.UI.Page
13{
14    protectedvoid Page_Load(objectsender, EventArgs e)
15    {
16        if(!IsPostBack)
17        {
18            this.GridView1.DataSource =new string[] {"Dotblogs", "F6 Team", "puma"};
19            this.GridView1.DataBind();
20        }
21    }
22    protectedvoid GridView1_RowDataBound(objectsender, GridViewRowEventArgs e)
23    {
24        if(e.Row.RowType == DataControlRowType.DataRow)
25        {
26            LinkButton lnkbtn = e.Row.Cells[0].FindControl("LinkButton1")as LinkButton;
27            lnkbtn.OnClientClick =string.Format("GetInfo('{0}'); return false;", e.Row.Cells[1].Text);
28        }
29    }
30}

執行結果:

原创粉丝点击