如何在javascript之前调用代码 [英] How to call code behind before javascript
问题描述
我有2页
第1页包含一个asp按钮,它应该调用函数后面的代码然后打开page2作为弹出窗口
on page2还有一个asp按钮应该从自己的代码后面调用一个函数而另一个函数从page1代码后面然后关闭弹出窗口
换句话说:我需要传输数据来自page1到page2,并在关闭之前准备page2中的另一个数据,并通知page1关于关闭以接收数据
注意:我想在没有用户权限的情况下执行此操作,所以从代码注册脚本?不是我要找的!
我用会话来传输数据
我尝试了什么:
我试图使用javascript调用隐形asp按钮点击事件
它适用于page1但是并不总是
page1使用母版页
page2 webform
在这段代码中,我使用了
I have 2 pages
page 1 contains an asp button which should call a code behind function then open the page2 as a popup window
on page2 there is also an asp button which should call a function from its own code behind and another function from page1 code behind then close the popup window
in other words: I need to transfer data from page1 to page2, and prepare another data in page2 before closing and inform page1 about closing to receive the data
note: I want to do this without user permission for the popup, so registering scripts from code behind? not what I am looking for!
"I use sessions to transfer data"
What I have tried:
I tried to call an invisible asp buttons click event using javascript
It works in page1 but not always
"page1 using master page"
"page2 webform"
At this code I used
document.getElementById('ContentPlaceHolder1_HiddenButton').click();
在打开弹出窗口之前创建数据会话。并使用
in page1 to create the data session before opening the popup. and used
document.getElementById('HiddenButton').click();
在第2页中准备另一个数据会话以便返回。还使用了
in page2 to prepare another data session for return. also used
opener.document.getElementById('ContentPlaceHolder1_HiddenButton1').click();
告知第1页关于新数据会话和弹出结束的信息。
page1 code < br $>
in page2 to inform page1 about new data session and popup closing.
page1 code
<script type="text/jscript">
function OpenPopup() {
document.getElementById('ContentPlaceHolder1_HiddenButton').click();
window.open("WebForm1.aspx", "List", "scrollbars=yes,resizable=no,width=1000,height=600");
return false;
}
</script>
<asp:Button ID="HiddenButton1" runat="server" Text="Button" style="display:none"
OnClick="HiddenButton1_Click"/>
<asp:Button ID="HiddenButton" runat="server" Text="Button" style="display:none" OnClick="HiddenButton_Click"/>
<asp:Button ID="btnOffer" runat="server" Text="Add Offer" onclientclick="javascript:return OpenPopup();"/>
page2 code
page2 code
<script type="text/javascript">
function returnValues() {
document.getElementById('HiddenButton').click();
opener.document.getElementById('ContentPlaceHolder1_HiddenButton1').click();
window.close();
}
</script>
<asp:Button ID="HiddenButton" runat="server" Text="Button" style="display:none" OnClick="HiddenButton_Click"/>
<asp:Button ID="btnFinish" runat="server" OnClientClick="javascript:return returnValues()" Text="finish and close" />
推荐答案
我刚刚在父页面(page1)上创建了一个函数,如下所示:
I just created a function on parent page(page1) as below:
function InformedFromPage2() {
alert('from page2');
}
在第2页,我调用了以下函数:
And on page 2 I called the function as followed:
function returnValues() {
opener.InformedFromPage2();
document.getElementById('HiddenButton').click();
opener.document.getElementById('HiddenButton1').click();
window.close();
}
为什么不学习和使用AJAX来调用服务器方法。这样你就不会处理回调,这可能会让你在与JavaScript逻辑混合时感到痛苦。这是一个可以帮助您入门的快速参考:使用jQuery AJAX和ASP.NET与数据库进行通信的许多方法 [ ^ ]
why not learn and use AJAX to invoke server methods. That way you won't be dealing with postbacks which could cause you pain when mixing with JavaScript logic. Here's a quick reference that may help you get started: Many ways to communicate with your database using jQuery AJAX and ASP.NET[^]
这篇关于如何在javascript之前调用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!