Solved

Execute javacript from code behind

Posted on 2014-09-12
4
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

739 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