GethuJohn
GethuJohn

Reputation: 233

Asp.net: How to call a javascript function at the end of button click code behind

My motto is to call a Java script function at the end of button click code behind. ie, firstly i need to execute the server side function after which my java script function should get invoked.

My server side method is as follows

protected string SaveEmbedURL_click()

{

    if (txtembedurl.Text != null)
    {
        School aschool = new School();
        aschool.SchoolId = CurrentSchool.SchoolId;
        aschool.EmbedUrl = txtembedurl.Text;
        SchoolRespository.updateEmbedUrl(aschool);
        return "true";
    }

}

My Java script function is as follows

function SaveEmbedUrlClientSide() {

admin_CustomizeTheme.SaveEmbedURL_click(true);
$('#lbl_embedcode').removeClass('hide').addClass('show');
$('#embedCode').removeClass('hide').addClass('show');
CopyToClipboard("embedCode");

}

How can i achieve this?

Thanks.

Upvotes: 0

Views: 8718

Answers (2)

Tim B James
Tim B James

Reputation: 20364

Page.RegisterStartupScript is now obsolete, so I would use this code.

ClientScript.RegisterStartupScript(Page.GetType, "Javascript", "SaveEmbedUrlClientSide();", true);

RegisterStartupScript requires Type, Reference, Code, render script blocks. Reference Here

Upvotes: 1

Conrad Frix
Conrad Frix

Reputation: 52645

I'm pretty sure all you need is to add this

RegisterStartupScript("YourJavaScript", "SaveEmbedUrlClientSide()");

"YourJavaScript" is an arbitrary string that is used to identify the Javascript.

Here's the relevant MSDN article.

Upvotes: 1

Related Questions