How to call Jquery function from C#

后端 未结 3 545
萌比男神i
萌比男神i 2020-12-19 13:23

I would like to fadein and out a div after performing some code. This JQuery function is working fine when using the button\'s onclientclick property. But, Unable to call it

相关标签:
3条回答
  • 2020-12-19 13:29

    When using asynchronous post backs is good to understand the events that you can actually handle on the client side (aspx file).

    If you want to execute some client function (jquery, javascript) after an asynchronous post back has been made, I suggest using the pageLoaded event on the client side. So your javascript will be something like the following:

    function pageLoad(sender, args) { 
        $(".saved").fadeIn(500).fadeOut(500);
    }
    

    For more information please refer to this MSDN page on the Event Order for Common Scenarios section.

    Hope this helps


    Edit after comment to add filter example

    To filter logic in the pageLoad event depending on the element that raised the event, as stated in this MSDN page, on the Animating Panels section, you can do something like this:

    var postbackElement;
    
    function beginRequest(sender, args) {
        postbackElement = args.get_postBackElement();
    }
    function pageLoaded(sender, args) {
        if (typeof(postbackElement) === "undefined") {
            return;
        } 
        else if (postbackElement.id.toLowerCase().indexOf('btnsave') > -1) {
            $(".saved").fadeIn(500).fadeOut(500);
        }
    }
    
    0 讨论(0)
  • 2020-12-19 13:33

    Try this...

    ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "Confirm();", true);
    
    0 讨论(0)
  • 2020-12-19 13:42

    C#

    protected void btn_click(object sender, Eventargs e)
    {
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
    }
    

    JS:

    <script>
       function Confirm()
       {
          $(".saved").fadeIn(500).fadeOut(500);
       }
    </script>
    

    Edit Improve Code

    C#

    protected void btn_click(object sender, Eventargs e)
    {
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
    }
    

    JS:

    <script>
        $(document).ready(function () {
    
           function Confirm() {
               $(".saved").fadeIn(500).fadeOut(500);
           };
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题