在网上找了些关于ASP.NET使用模态窗口(showModalDialog)的例子,都不是很好用,通常的问题为,在弹出的模态窗口中如何点一下服务器端的Button(有回发事件执行)时,就会弹出和此窗口一样的新窗口并且不在是模态窗口,感觉不能在弹出的窗口中使用任何服务端事件,只能用一些客户端事件及JS脚本控制,这是asp.net中自提交页面造成的.这样的模态窗口根本不能满足要求.我要实现一个真正的模态窗口,在模态窗口中可以做任何与普通页面一样的功能.还有一种实现模态窗口方法就是使用框架结构(frameset)实现起来没有问题,就是麻烦点,要提供一个框架页面.
经过多方面总结我实现了个真正的模态窗口,也是使用框架页面实现,不过是个通用的过程.不增加任何多余的页面。支持传递参数,窗口标题自定义,可以使用showModalDialog或showModelessDialog.
1,前台代码,没有实现任务功能,只是为了提供页面请求
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="modalframe.aspx.cs" Inherits="WebApplication1.modalframe" %>
- <!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 runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>
2,后台C#
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApplication1
- {
- public partial class modalframe : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string st, atitle, myurl;
- if (!IsPostBack)
- {
- int ind;
- st = Request.RawUrl;
- ind = st.ToLower().IndexOf("src=");
- if (ind >= 0)
- {
- st = st.Substring(ind + 4); //, st.Length - ind - 4);
- //st = st.Replace("src=", "");
- }
- else
- {
- st = "about:blank";
- }
- myurl = st;
- atitle = Request.RawUrl;
- ind = atitle.ToLower().IndexOf("title");
- if (ind >= 0)
- {
- // '得到title
- st = Request.RawUrl;
- int indx2 = st.ToLower().IndexOf("src=");
- atitle = atitle.Substring(ind + 6, indx2 - 1 - ind - 6);
- if (atitle == "")
- atitle = "『dacong』";
- }
- else
- {
- atitle = "『dacong』";
- }
- Response.Write("<!DOCTYPE html PUBLIC /"-//W3C//DTD XHTML 1.0 Transitional//EN/" /"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/">");
- Response.Write("<html xmlns=/"http://www.w3.org/1999/xhtml/">");
- Response.Write(" <head>");
- Response.Write(" <title>" + atitle + "</title>");
- Response.Write(" </head>");
- Response.Write(" <frameset border='0' frameSpacing='0' rows='254' frameBorder='0'>");
- Response.Write(" <frame name='mainframe' src=" + "/"" + myurl + "/"" + ">");
- Response.Write(" </frameset>");
- Response.Write("</html>");
- Response.Flush();
- Response.End();
- }
- }
- }
- }
3测试页面,js代码
- function Button6_onclick() {
- var TmpURLStr = "modalframe.aspx?src=webform2.aspx?id=dacong";
- //var TmpURLStr = "modalframe.aspx?src=webform2.aspx?id=dacong&id2=p1"; //多参数
- //var TmpURLStr = "modalframe.aspx?title=李大聪&src=webform2.aspx?id=dacong"; //增加标题
- var sFeatures = 'center=yes;dialogWidth:800px;dialogHeight:400px;toolbar=no,scrollbars=no;status:no; help:no;scroll:no;resizable:no';
- var str = window.showModalDialog(TmpURLStr, 'order_maintain', sFeatures); //执行模态;
- //var str = window.showModelessDialog(TmpURLStr, 'order_maintain', sFeatures); //执行非模态;
- //alert(str);
- }