Solved

Execute javacript from code behind

Posted on 2014-09-12
4
291 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bootstrap question 4 26
web page freezes after ajax post 7 44
JQuery - Default innerhtml text with keyup function (copy as you type) 3 15
C# LINQ 5 22
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

756 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