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
Solved

Add javascript from code behind page c#

Posted on 2014-11-21
7
370 Views
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'}); ");
            sb.Append("</script>");
            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
0
Comment
Question by:SiemensSEN
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

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

Accepted Solution

by:
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);
0
 

Author Comment

by:SiemensSEN
ID: 40457910
Thanks .. so I don't need to add <asp:Literal ID="ltrlTop" runat="server"></asp:Literal> to my ascx page?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 40

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Closing Comment

by:SiemensSEN
ID: 40458247
Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Diminish Pop-up  in 3 seconds 7 44
Html CheckBox obtain Its Value 5 27
How to resize a div in html 3 35
binding event to form in window.open 9 35
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

791 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