How to run a javascript function before postback of asp.net button?
        Posted  
        
            by Curtis White
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Curtis White
        
        
        
        Published on 2010-05-27T16:03:24Z
        Indexed on 
            2010/06/07
            13:52 UTC
        
        
        Read the original article
        Hit count: 232
        
ASP.NET
|JavaScript
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
© Stack Overflow or respective owner