Link to home
Start Free TrialLog in
Avatar of webressurs
webressursFlag for Norway

asked on

Execute javacript from code behind

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?
ASKER CERTIFIED SOLUTION
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of webressurs

ASKER

Is it possible to solve this by placing the JavaScript code front, and call it from code behind?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial