简体   繁体   中英

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. 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?

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.

Update: It looks like the Javascript function is called before the postback but the element is not updated until I run the second postback. Hmm

Update: Unfortunately it is a bit more complicated then this.

I'm creating a div tag in javascript to open a new window. Inside the div tag, I'm using a databinding syntax <%=Preview%> so that I can get access to this element on the server side. From the server side, I'm injecting the code.

I'm thinking this may be a chicken-egg problem but not sure.

UPDATE!

It is not the Javascript not running first. 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. you can gain finer control of your app by posting from javascript whenever you are ready..

the framework auto generates a function called __doPostback(.....) and eventually calls it every time it needs to do a postback.

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.

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

enter code hereWould this work for you?

Javascript

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

.NET code

<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.

If you want to use jquery you could do the following:

$(document).ready(function() {

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

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

        eval(execScript);
    });
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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