开发者

How to trigger onclick event after received return value from popup which using add attribute a javascript?

How to trigger onclick event after received return value from popup aspx which using add attribute a javascript?

After adding .Attributes.Add("onClick", "return popWin('" + New开发者_运维技巧BatchNo_TextBox.Text + "');");

Original onclick event do not fire?

if this method can not work, any other method to get value from pop up message box and return value and run click event

Thanks.


try this

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>


The same problem I faced several times the cause is that java script cannot call code behind(c#/vb function/events).

An alternate way I used is to use hidden fields that is accessible by both java script and code behind. But then you want to call events not read values modified by java script.

For this we refreshed the page through java script when we want to trigger that code behind event and let the page load to monitor the scenario(value that java script set) and triggers the required event.

The code at the end would be real mess and unmanageable, would take lots of efforts to debug.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜