[RESOLVED]Execute JQuery After Postback

Hey, I’m trying to run a simple jquery script. I’ve got it working without a postback but I want to be able to click a linkbutton, have a postback and then have the jquery execute.

Here’s the script for context:

$(document).ready(function () {
    $("[id$=lnkRemove]").click(function () {
        $dataribbon = $(".ribbon-content");
        $dataribbon.delay(500).slideDown(500).delay(2500).slideUp(500);
    });
});

I’ve read about ScriptManager and it has this that could be useful:

ScriptManager.RegisterStartupScript

I’m not entirely sure how that works and other than that, there doesn’t seem to be a lot of resources for running javascript/jquery after a postback. I’ve also read a bit about UpdatePanels and partial postbacks. Can anyone tell me the best way to approach
this?

Make your js code a normal function

function myFunction() {
    $("[id$=lnkRemove]").click(function () {
        $dataribbon = $(".ribbon-content");
        $dataribbon.delay(500).slideDown(500).delay(2500).slideUp(500);
    });
}

In the click event handler for your linkbutton, use RegisterStartupScript to write js to call the "myFunction" function.  Google for RegisterStartupScript for examples.

Hi FireVulpes,

Thanks for your post.

As for your problem, you could refer to the following sample to call a javascript function from server side.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function myFunction() {
            alert("Hello World");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="btnok" runat="server" OnClick="btnok_Click">CallClientSideFunction</asp:LinkButton>
    </div>
    </form>
</body>
</html> 
protected void btnok_Click(object sender, EventArgs e)
{
    //your code
            
    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "myFunction();", true);
}

If you have any question about this issue, please post back freely.

Best Regards,

Fei Han

Leave a Reply