views:

19

answers:

2

Hi friends,

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.

+1  A: 

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.

Conrad Frix
Thanks conrad. But i think i am missing some parameter while using RegisterStartupScript. Can u explain how RegisterStartupScript should be used in my case?
jithin
RegisterStartupScript, which is Page.RegisterStartupScript is now obsolete so I wouldn't use this method anymore.
Tim B James
+1  A: 

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

Tim B James