2011-02-25 33 views
0

如何触发onclick事件后收到来自popup使用添加属性JavaScript的弹出窗口的值?如何触发onclick事件后收到从弹出使用添加属性JavaScript的返回值?

添加后 .Attributes.Add(“onClick”,“return popWin('”+ NewBatchNo_TextBox.Text +“');”);

原始onclick事件不会触发?

如果此方法不能工作,任何其他方法来得到弹出消息框和返回值的值和运行单击事件

感谢。

+0

如果我错了,请纠正我。从子窗口获得价值后,你想要做回传? – 2011-02-25 08:55:50

回答

1

试试这个

Main.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %> 

<!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> 
    <script type='text/javascript'> 

     function DoStuff() { 
      document.getElementById('Button1').click(); 
     } 
     function popWin() { 
      var popy = window.open('popup.aspx', 'popup_form', 'menubar=no,status=no,top=100%,left=100;') 

     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:TextBox ID="txtPopupValue" runat="server" Width="327px"></asp:TextBox> 
     <asp:Button ID="Button1" runat="server" Text="Show List" /> 
    </div> 
    </form> 
</body> 
</html> 

Main.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Main : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 

      this.Button1.Attributes.Add("onclick", "return popWin()"); 

     } 
    } 
} 

popup.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="popup.aspx.cs" Inherits="popup" %> 

<!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> 
    <script type='text/javascript'> 
     function validepopupform() { 
      window.opener.document.getElementById('txtPopupValue').value = document.getElementById('txtPop').value 
      self.close(); 
     } 
     window.onbeforeunload = CloseEvent; 
     function CloseEvent() { 
      if (window.opener && !window.opener.closed) { 
       window.opener.DoStuff(); 
      } 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:TextBox ID="txtPop" runat="server"></asp:TextBox> 
     <input type='button' value='go' onclick='validepopupform()' /> 
    </div> 
    </form> 
</body> 
</html> 
+0

试过后,点击事件不会在dostuff中触发 – Jo0o0 2011-02-25 09:56:31

+0

谢谢。它的工作原理,当我试图添加一个按钮,但看不见这个按钮将不会被触发 – Jo0o0 2011-02-25 10:06:32

+0

最后我设置额外的按键宽度= 1像素和白色 – Jo0o0 2011-02-25 10:08:36

1

我遇到同样的问题几次,原因是java脚本无法调用后面的代码(c#/ vb函数/事件)。

我使用的另一种方法是使用隐藏的字段,可以通过java脚本和后面的代码访问。但是,然后你想调用事件不读取由Java脚本修改的值。

为此,我们通过刷新Java脚本的网页时,我们要触发事件背后的代码,让页面加载到监测方案(值Java脚本设置),并触发所需的事件。

最后的代码将是真正的混乱和难以管理,需要大量的努力来调试。

+0

谢谢。我使用cookie而不是隐藏字段。关键是使用window.opener.location.reload();在子窗口 – Jo0o0 2011-02-25 09:22:07

+0

设置过期后cookie无法正常工作并检查过期> datetime.now,该怎么办? – Jo0o0 2011-02-25 09:55:08