0

C# Trigger a js function with parameter from code behind. I have the following code:

C#:

 ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptManager1", String.Format(@"ShowHideMessageBlock('{0}')", @"#successMsg"), true);

js:

function ShowHideMessageBlock(xid) {
    var c = xid;
    console.log(c);

    $(c).fadeIn('slow', function () {
        $(this).delay(5000).fadeOut('slow');
    });
}

When I open the console window I get the following message: Uncaught SyntaxError: Unexpected identifier

The rendered function is now:

<script type="text/javascript">
//<![CDATA[
ShowHideMessageBlock('#successMsg')Sys.Application.add_init(function() {
    $create(Sys.UI._UpdateProgress, {"associatedUpdatePanelId":null,"displayAfter":500,"dynamicLayout":true}, null, null, $get("updateProgress"));
});
//]]>
</script>

Can somebody help me with this issue. (It worked in the past) maybe I have changed/broken something and it is not working anymore.

1
  • Where is that Sys.Application.add_init stuff coming from? Also, I think ShowHideMessageBlock() should end with a semicolon. Commented Jun 23, 2015 at 15:33

1 Answer 1

1

All you need to do is add a semi-colon to the end of your String.Format call.

ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptManager1", 
    String.Format(@"ShowHideMessageBlock('{0}');", @"#successMsg"), true);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks man that worked. It has been a long day. How did I not see that
@Babulaas Sometimes it just takes a fresh pair of eyes ;)
Thanks much! I was breaking my head to see what went wrong but this missing semicolon fixed it all in 2020 as well :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.