Add javascript from code behind page c#

Posted on 2014-11-21
Last Modified: 2014-11-21
Hello ,
  How do I include some js code in mt ascx page?

In my ascx page I have a tag
<asp:Literal ID="ltrlTop" runat="server"></asp:Literal>

Open in new window

In my code behind (ascs,cs) file

 private string generatePreScript()
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>");
            sb.Append("dataLayer.push({'event': 'pageview', 'pageview':'/vp/apply.html'}); ");
            return sb.ToString();

Open in new window

I want set the ltrlTop  to the js value

tried  ltrlTop.Text = generateSuccesScript(); but I get dataLayer is undefine

Thanks for your help
Question by:SiemensSEN
  • 3
  • 3
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40457745
Have you looked at ScriptManager.RegisterClientScriptBlock yet?
LVL 40

Accepted Solution

Kyle Abrahams earned 500 total points
ID: 40457890
Just put this line of code where you want it to be called.  After the page is rendered the JS will fire.  

ScriptManager.RegisterStartupScript (this, this.GetType(), "MyTopCodeBlock", "dataLayer.push({'event': 'pageview', 'pageview':'/vp/apply.html'}); ", true);

Author Comment

ID: 40457910
Thanks .. so I don't need to add <asp:Literal ID="ltrlTop" runat="server"></asp:Literal> to my ascx page?
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40457923
nope.  The registerStartupScript will inject (and fire) the javascript code.

Author Comment

ID: 40457987
Thanks,  but i still get the error  dataLayer is undefined
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40458072
that's a javascript error.  where is your dataLayer var declared?

Author Closing Comment

ID: 40458247

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

821 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