Soufiaane
Soufiaane

Reputation: 2087

call JavaScript function from code behind

I have this JavaScript code in an ASPX page

<script>
    function show_modal(statut) 
    {
        if (statut == true)
        {
            $(function () 
            {
                $('#modal_success').modal('show')
            })
        }
        else
        {
            $(function ()
            {
                $('#modal_fail').modal('show')
            })
        }
     }
</script>

That shows an modalpopup which I like to launch from my code behind.

I tried this, but it didn't work:

if (resultat)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "show_modal(true);");
            
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "show_modal(false);");
        }

but I can't figure out why!

Upvotes: 0

Views: 637

Answers (3)

Ashish
Ashish

Reputation: 29

Try using ScriptManager.RegisterClientScriptBlock Method => it should work and for more check the Error console for tracing your issue.

Upvotes: 0

stuartd
stuartd

Reputation: 73243

This call requires you to wrap the call in a <script> tag (or use the other overload which allows you to specify if script tags are added)

ClientScript.RegisterStartupScript(this.GetType(), "", 
                "<script>show_modal(true);</script>");

or

ClientScript.RegisterStartupScript(this.GetType(), "", 
                "show_modal(true);", true);

Upvotes: 4

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

Try:

ScriptManager.RegisterStartupScript(this.Page, typeof(Page), Guid.NewGuid().ToString(), script, true);

Upvotes: 1

Related Questions