简体   繁体   English

如何在回发asp.net按钮之前运行javascript函数?

[英]How to run a javascript function before postback of asp.net button?

I'm using Javascript to create a DIV element and open up a new page by using onclientclick. 我正在使用Javascript创建一个DIV元素并使用onclientclick打开一个新页面。 This works great. 这非常有效。 Now, I need to write to it from the server side and this element must be created before it is posted back. 现在,我需要从服务器端写入它,并且必须在回发之前创建此元素。

How do I get the javascript to execute before the postback? 如何在回发之前获取javascript?

Currently, I have to press the button twice because the element doesn't exist to write too on the first click. 目前,我必须按两次按钮,因为在第一次点击时也不存在要写的元素。

To be clear, I need this to execute before the "OnClick" of the button. 为了清楚起见,我需要在按钮的“OnClick”之前执行此操作。

Update: It looks like the Javascript function is called before the postback but the element is not updated until I run the second postback. 更新:看起来在回发之前调用了Javascript函数,但在运行第二个回发之前元素没有更新。 Hmm

Update: Unfortunately it is a bit more complicated then this. 更新:不幸的是它比这复杂一点。

I'm creating a div tag in javascript to open a new window. 我正在javascript中创建一个div标签来打开一个新窗口。 Inside the div tag, I'm using a databinding syntax <%=Preview%> so that I can get access to this element on the server side. 在div标签内部,我使用数据绑定语法<%= Preview%>,以便我可以在服务器端访问此元素。 From the server side, I'm injecting the code. 从服务器端,我正在注入代码。

I'm thinking this may be a chicken-egg problem but not sure. 我想这可能是鸡蛋问题,但不确定。

UPDATE! UPDATE!

It is not the Javascript not running first. 它不是Javascript没有先运行。 It is the databinding mechanism which is reading the blank variable before I'm able to set it. 它是数据绑定机制,它在我能够设置它之前读取空白变量。

Hmm

you don't have to rely on server controls to perform postbacks in asp.net. 您不必依赖服务器控件来在asp.net中执行回发。 you can gain finer control of your app by posting from javascript whenever you are ready.. 只要您准备好,就可以通过javascript发布来更好地控制您的应用。

the framework auto generates a function called __doPostback(.....) and eventually calls it every time it needs to do a postback. 框架自动生成一个名为__doPostback(.....)的函数,并在每次需要进行回发时最终调用它。

so. 所以。 instead of using server control button, you can have a regular <button onclick="foo()" /> than once you're done performing all that you need, just call the __doPostback function. 而不是使用服务器控制按钮,你可以有一个常规的<button onclick="foo()" />不是一旦你完成所需的一切,只需调用__doPostback函数。

asp.net gives you a nifty way to access that function with Page.GetPostbackClientEvent (i believe, but there are couple methods that support this methodology) http://msdn.microsoft.com/en-us/library/system.web.ui.page.getpostbackclientevent.aspx asp.net为您提供了一种使用Page.GetPostbackClientEvent访问该功能的好方法(我相信,但有几种方法支持这种方法) http://msdn.microsoft.com/en-us/library/system.web。 ui.page.getpostbackclientevent.aspx

enter code hereWould this work for you? 在这里输入代码这对你有用吗?

Javascript 使用Javascript

function stuffYouWantToDo(){
  //do your stuff
  //then submit the .NET form
  document.forms[0].submit();
}

.NET code .NET代码

<asp:button ID="Button1" onClientClick="return false;stuffYouWantToDo();"/>

This should ensure that the stuff you want to do is done, then the form will be submitted. 这应该确保你想要做的事情完成,然后提交表格。 The return false prevents the form from being submitted when you first click the button and relies on the call in the stuffYouWantToDo function to submit the form. 返回false会阻止在您第一次单击按钮时提交表单,并依赖stuffYouWantToDo函数中的调用来提交表单。

If you want to use jquery you could do the following: 如果要使用jquery,可以执行以下操作:

$(document).ready(function() {

    var execScript = $(".myButton").attr("href").replace("javascript:", "");

    $(".myButton").attr("href", "#").click(function() {
        // Add before click logic here

        eval(execScript);
    });
});

难道你不能简单地在表单中的某处添加自定义验证器吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM