利用.NET代碼實現點擊按鈕彈出新窗體,輸入數據后返回並刷新頁面,網上大部份都是JavaScript實現,為使用方便,本人將其封裝進一段.NET代碼中。只所以稱作.net代碼,是因為C#/VB各一份嘍:)
也是因為自己的項目中要用到一項類似於Winform的Messagebox.ShowDialog這樣的功能,起初以為跟Winform差不了多少的,誰知做的時候卻出現了很多"難以預料"的問題,本著解決問題的程序員的態度,花了一個下午的時間,將遇到的問題基本上全解決了。記錄一下,或許會有朋友也存在類似問題,為免少走一些元枉路,索引再花點時間整理了一下。
以下講解部分使用VB講解,喜歡C#的可以在隨后的附件中下載C#版本。
首先新建一個Default.aspx文件,在aspx Source中輸入:
Default.aspx

<%
@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMessage" runat="server" Font-Bold="True">Open a New Page Form , and transport a Variant to the new page</asp:Label><br>
<br>
<asp:TextBox ID="tbxMsg" runat="server" Width="431px">Here are the Variant 's value</asp:TextBox><br>
<br>
<asp:Button ID="btnOK" runat="server" Text="OK" Width="96px"></asp:Button>
</div>
</form>
</body>
</html>
然后轉至
CodeFile輸入代碼:
Default.aspx.vb

Partial Class _DefaultClass _Default
Inherits System.Web.UI.Page


Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not ClientScript.IsClientScriptBlockRegistered("clientScript")) Then
Dim strScript As String = "<script>" + vbCrLf
strScript += "function OpenWin(){" + vbCrLf
strScript += "var str=window.showModalDialog('Default2.aspx',document.getElementById('tbxMsg').value)" + vbCrLf
strScript += "if(str!=null) document.getElementById('tbxMsg').value=str" + vbCrLf
strScript += "}" + vbCrLf
strScript += "</script>" + vbCrLf
ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScript", strScript)
End If
btnOK.Attributes.Add("onclick", "OpenWin()")
End Sub
End Class
很明顯,我上面只是簡單的string拼接。記得曾經在某網站看到說用StringBuilder的優於直接String的拼接吧。原理上是的,如果你愿意,就自行改用StringBuilder吧
接下來再加一個aspx文件,直接在aspx Source中輸入如下:
Default2.aspx

<%
@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!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>Untitled Page</title>
</head>
<frameset rows="0,*">
<frame src="about:blank">
<frame src="Default3.aspx">
</frameset>
</html>
這個比較簡單,CodeFile就沒有什么啦。
加入第三個aspx, aspx Source如下:
Default3.aspx

<%
@ Page Language="VB" AutoEventWireup="false" CodeFile="Default3.aspx.vb" Inherits="Default3" %>

<!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>Untitled Page</title>
</head>
<body id="MyBody" runat="server">
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMessage" runat="server" Width="435px">Please enter some words:</asp:Label><br>
<br>
<asp:TextBox ID="tbxMsg" runat="server" Width="431px"></asp:TextBox><br>
<br>
<asp:Button ID="btnOK" runat="server" Text=" OK " Width="96px"></asp:Button>
</div>
</form>
</body>
</html>
註意一下
<body id="MyBody" runat="server">這句話,vs2005默認產生的Document的body是沒有id,runat的,這里請手工加上去。
CodeFile如下:
Default3.aspx.vb


Partial Class Default3Class Default3
Inherits System.Web.UI.Page


Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
Dim strScript As String = "<script>" + vbCrLf
strScript += "window.parent.returnValue='" + tbxMsg.Text.Replace("'", "\'") + "'" + vbCrLf
strScript += "window.parent.close()" + vbCrLf
strScript += "</script>" + vbCrLf
If (Not ClientScript.IsClientScriptBlockRegistered("clientScript")) Then
ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScript", strScript)
End If
End If
If Not IsPostBack Then
MyBody.Attributes.Add("onload", "document.getElementById('tbxMsg').value=window.parent.dialogArguments")
End If
End Sub
End Class
基本上就是這樣子的,做完之后感覺很簡單,呵呵。當然,這個也沒有花很多時間,因為網上的現成代碼很多的。見隨后的參考文獻2
所花時間多的是隨后,將其放入真實項目中的時候,真實項目中,由於使用到了2.0的新加功能MasterPage,這下好了,將上面代碼原封不動的拷進去,總沒有效果出來,最后不得以設定了一下調試,說什么獲取不到對像,原話是什么,我也不記得,大概意思就是這樣子了,現在已經沒有了錯誤,我總不會為了重現這個錯誤,再試一下吧。呵呵:)如果你有興趣見識的話,將下載的原碼中
document.getElementById('" + tbxMsg.ClientID + "').value這句話改成document.getElementById(' tbxMsg').value就可看到效果了。
其實,我能夠想到加ClientId也是帶點僥幸的意味的。因為有VS2005強勁的JavaScript調試功能才得以得逞:)。調試時我發現在頁面生成之後,原來的頁面中的tbxMsgx id被改成類似這樣一個東西 ctl00$ContentPlaceHolder1$tbxMsg1於是就用這個代替了原先的tbxMsg,當時程序沒有報錯,也就是通過了,效果也出來了。呵呵,於是我記起WebUI Control好像都有一個什么ClientID的屬性。應該就類似於上面ctl00$ContentPlaceHolder1$tbxMsg1的東西吧。於是就用tbxMsg.ClientID試了一下。居然正確,有點歪打正著的感覺!竊喜ing.......:)
下載文件當中的MasterPage參考了CodeProject上的一篇文章,見參考文獻1.
說這些說了這麼多,也不知道你有沒有耐心看下去,還是附上解決方案吧,看Demo可能有些人覺得更實在。
模式窗體傳值源碼下載
參考文獻:
1、
Creating a Common Toolbar in ASP.NET 2.0 using MasterPage, Delegate and Events.
2、
优快云明飛的專欄