Execute javacript from code behind


I have an asp.net page that is called from an externat system (credit card company). When the asp.net file is requested I need to make a JavaScript Call from the asp.net code behind file. The problem is that the JavaScript never runs.

Asp.net code front:

<asp:PlaceHolder runat="server" ID="plhAnalytics" />

Open in new window

Asp.net code behind:

protected void Page_Load(object sender, EventArgs e)
  if ([A LOT OF STUFF])

protected void TrackAnalytics()
    PlaceHolder plhAnalytics = this.plhAnalytics;
    HtmlGenericControl objScript = new HtmlGenericControl("script");
    objScript.Attributes.Add("type", "text/javascript");

    System.Text.StringBuilder strScript = new System.Text.StringBuilder();
    strScript.Append("analytics.identify('1234567890', {\n");
    strScript.Append("plan: 'Paid'\n");
    strScript.Append("analytics.track('Payment Created', {\n");
    strScript.Append("category: '" + type + "'\n");

    objScript.InnerHtml = strScript.ToString();

Open in new window

How can I Call this JavaScript for code behind?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
You can't. Javascript is client side and executed by the browser. The code behind is server side and executed before the page is built or after a post back.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
javascript run on the browser, your server need to return a page to the user to run the javascript.
if you're in a payment process you can't
webressursAuthor Commented:
Is it possible to solve this by placing the JavaScript code front, and call it from code behind?
no, you can execute code behind from javascript but not calling directy javascript from code behind
you can only send it for the page REQUESTED by the user browser
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.