ASP.NET asynchronous post-back on asp:button click

后端 未结 1 719
小蘑菇
小蘑菇 2021-01-20 16:03

I am using AJAX in asp:net and am trying to handle a situation where an asp:button triggers both a javascript method and a codebehind c# method. To do this I am using onclic

相关标签:
1条回答
  • 2021-01-20 16:19

    If you simply want to run your OnClientClick (client) code before your OnClick (server) code, try the following:

    ASP.NET

    <asp:Button ID="btn_mybutton" runat="server" Text="MyButton" OnClientClick="return JSFunction();" OnClick="CFunction"/>
    

    Javascript

    <script type="text/javascript">
        function JSFunction() {
            alert('Some JavaScript stuff');
            return true; 
        }
    </script>
    

    C#

    protected void CFunction(object sender, EventArgs e)
    {
        // Some server code
    }
    

    The JavaScript function is executed first and returned from before the server code is executed.

    0 讨论(0)
提交回复
热议问题