Trigger a js function with parameter from code behind

999 views Asked by At

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

There are 1 answers

3
Jon Grant On BEST ANSWER

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);