Calling javascript function from within code using ClientScriptManager

453 views Asked by At

I have the following script tag included on the _layout page of my MVC application:

<script src="https://backpack.openbadges.org/issuer.js"></script>

This is used to issue badges on completion of courses. Thus far I have been issuing badges directly from the Views using javascript such as:

var postData = {
  'badgeClassID': 1
};

$.ajax({
  type: "GET",
  cache: false,
  url: "/Admin/NewBadgeAssertion",
  data: postData,
  success: function (dataBA) {
     var myURL = dataBA;
     OpenBadges.issue([myURL]);
  },
  error: function (error) {
     alert("An Error has occurred during the Issue of this OpenBadge");
  }
});

This is working for elearning courses that I have created but the completion of SCORM courses are recorded on close of the window and I now want to issue badges for SCORM courses through C# code behind rather than from the browser view.

How can I call OpenBadges.issue([myURL]) from code behind using ClientScriptManager. I have found the RegisterStartupScript method but I am not sure how to derive the type, key and script values. I have tried this:

ClientScriptManager csm = new ClientScriptManager();
csm.RegisterStartupScript(GetType(), "msgbox", "alert('SCORM BADGE HAS BEEN ISSUED FOR SUCCESSFUL COMPLETION');", true); 

But I am not correctly defining any constructors

1

There are 1 answers

0
dave823 On

Try this:

First, on your view page, create a simple javascript function, which does what you need it to do, such as call OpenIssues.issue:

function myFunction(myUrl)
{
    alert('test:' + myUrl);
    OpenBadges.issue(myUrl);
}

And in your codebehind, you can call the javascript function:

String jscript = "myFunction('" + myUrl + "')"; //sending url as parameter
Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript",jscript,true);