Analytics

2010年8月24日 星期二

[Javascript]使用 ASP.NET 網頁傳值並觸發event(Use ASP.NET page by value and triggers event)


問題

使用 ASP.NET 網頁傳值並觸發event



解決方法

page1控制項:btn1,btn2,txtReturnValue
page2控制項:btnok

流程如下
由btn1觸發開page2的網頁=>page2按下btnok後傳txtReturnValue回page1,並觸發btn2的事件

page1 aspx 
   
<script language="javascript">    
function OpenPage()    
 {    
 var txtname =document.getElementById('txtname').value;    
 var txtReturn=document.Form1.txtReturn;    
 var strUrl;    
 txtReturn.value=""    
 strUrl="../page2.aspx?";    
 strUrl+="name="+ txtname;    
     
 var intheight=500;    
 var intwidth=750;    
 var PosTop=(screen.height-intheight)/2;    
 var PosLeft=(screen.width-intwidth)/2;     
 window.open(strUrl,'page2','toolbar=no,menubar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,top='+PosTop+',left='+PosLeft+',width ='+intwidth+',height ='+intheight);    
 return false;    
 }   
</script>    
'要讓javascript抓的到開啟視窗的主頁面物件,需要用標準的html物件歐!!    
<asp:textbox id="txtReturnValue" runat="server" Width="100%" MaxLength="500">    
<INPUT id="txtReturnValue" type="hidden" name="txtReturn" runat="server">    
<asp:textbox id="txtname" runat="server" Width="100%" MaxLength="500">    
    
  <asp:Button ID="btn1" runat="server" Text="開新網頁" />    
  <asp:button id="btn2" runat="server" Text="btn2" CssClass="button"     />
page1 vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me. btn1.Attributes.Add("onclick", "return OpenPage();")
End Sub
Protected Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    '要執行寫在這裡
ME. txtname.Text= txtReturnValue.Text
End Sub
page2 vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Expires = 0
        ViewState("name") = Request.QueryString("name")
End Sub
Protected Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        Dim strScript As String = ""
strScript = "if (window.opener!=null) {"
        strScript += "window.opener.document.getElementById('txtReturnValue').value='王曉明';"
        strScript += "var btn2=window.opener.document.getElementById('btn2');"
        strScript += "btn2.click();"
        strScript += "window.opener.focus();"
        strScript += "}"
        strScript += "window.opener=null; window.close();"
End Sub

沒有留言:

熱門文章