Solved

Execute javacript from code behind

Posted on 2014-09-12
4
283 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
ID: 40319330
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
ID: 40319334
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
ID: 40319859
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
ID: 40319900
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now