1.構想:WebForm1構建一個TextBox,一個HyperLink控件。WebForm3(為什麼是3不是2呢?下面說明)構建一個Calendar控件。當點擊WebForm1的HyperLink時,打開WebForm3。在WebForm3的Calendar控件中選擇日期後,關閉WebForm3,並且WebForm1中的TextBox獲取剛才所選擇的日期。 2.實現: 1) 網上有現成的日期選擇控件,但考慮到軟件的安全性和版權問題,還是選擇自己開發。 2) 在WebForm3中獲取日期後,將數值記錄到Session中,並且採用Javascript:window.opener.location.reload();window.close的方法刷新WebForm1。這種方法在WebForm1中只構建了一個Textbox的時候可以很方便的使用,但是如果構建了多個TextBox,刷新的時候,會將其他TextBox內容清空,故不可取。 3) 使用ShowModalDialog的方法傳值,具體方法如下。 3.具體做法:(部分代碼) WebForm1.aspx <form id=「Form1「 method=「post「 runat=「server「> <asp:textbox id=「textbox1「 runat=「server「></textbox> <asp:hyperlink id=「hyperlink1「 runat=「server「 navigateurl=「javascript:void(0)「 onclick='javascript:var str=window.showModalDialog(「webform2.aspx「);document.Form1.textbox1.value=str'></asplink> </form> WebForm2.aspx <html> <head></head> <frameset rows=「0,*「> <frame src=「about:blank「> <frame src=「WebForm3.aspx「> </frameset> <html> 註:為什麼要構建WebForm2呢?如果不構建WebForm2,直接打開WebForm3的話,在WebForm3中點擊Calendar空間選擇日期時,會彈出一個新窗口,致使程序無法按預期運行,具體為什麼會這樣,知識有限,搞不清楚..... :P WebForm3.aspx.vb Private Sub Calendar_selectionChanged(byval sender as object, byval e as system.eventargs) handles calendar.selectionchanged response.write(「<script language='javascript'>window.parent.returnvalue='「 & calendar.selecteddate.tostring & 「';</script>「) response.write(「<script language='javascript'>window.parent.close();</script「) End sub 4.後記 按照此方法類推,還可以在窗口之間傳遞其他值。 特別感謝孟憲會老師的相關文章。 |
2011年12月5日 星期一
利用頁面傳值獲取Calendar控件日期
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言