Solved

Execute javacript from code behind

Posted on 2014-09-12
4
276 Views
Last Modified: 2014-09-15
Hi!

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])
  {
    TrackAnalytics();
  }
}

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("});\n");
    strScript.Append("analytics.track('Payment Created', {\n");
    strScript.Append("category: '" + type + "'\n");
    strScript.Append("});\n");

    objScript.InnerHtml = strScript.ToString();
    plhAnalytics.Controls.Add(objScript);
}

Open in new window


How can I Call this JavaScript for code behind?
0
Comment
Question by:webressurs
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 167 total points
Comment Utility
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.
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 333 total points
Comment Utility
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
0
 
LVL 1

Author Comment

by:webressurs
Comment Utility
Is it possible to solve this by placing the JavaScript code front, and call it from code behind?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 333 total points
Comment Utility
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now