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

本文分享了一个在asp.net环境下实现showModelessDialogEX的实例,包括如何通过JavaScript调用该对话框,并提供了解决在MasterPage环境下可能遇到的问题的方法。测试结果显示此实现在IE7、Firefox3上可用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

asp.net(c#)

showModelessDialogEX.aspx

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<html xmlns="http://www.w3.org/1999/xhtml">
06<head id="Head1" runat="server">
07    <title>showModelessDialogEX</title>
08</head>
09<body>
10    <form id="form1" runat="server">
11    <div>
12        <asp:TextBox ID="TextBox1" runat="server" onclick='CallDialog();'></asp:TextBox>
13    </div>
14    </form>
15</body>
16</html>
17 
18<script type="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

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    protected void Page_Load(object sender, EventArgs e)
15    {
16 
17    }
18}

myDialog.aspx

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<html xmlns="http://www.w3.org/1999/xhtml">
05<head runat="server">
06    <title>myDialog</title>
07</head>
08<body>
09    <form id="form1" runat="server">
10    <div>
11        <asp:GridView ID="GridView1" runat="server"
12            onrowdatabound="GridView1_RowDataBound">
13            <Columns>
14                <asp:TemplateField HeaderText="Select">
15                    <ItemTemplate>
16                        <asp:LinkButton ID="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<script type="text/javascript">
27function GetInfo(str)
28{
29    var arg = window.dialogArguments;
30    arg.SetTextBox(str);
31    window.close(); 
32}
33</script>

myDialog.aspx.cs

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    protected void Page_Load(object sender, EventArgs e)
15    {
16        if (!IsPostBack)
17        {
18            this.GridView1.DataSource = new string[] { "Dotblogs", "F6 Team", "puma" };
19            this.GridView1.DataBind();
20        }
21    }
22    protected void GridView1_RowDataBound(object sender, 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}

執行結果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值