Solved

Add javascript from code behind page c#

Posted on 2014-11-21
7
345 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 74

Expert Comment

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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

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 39

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now