Insert google adword tracking code while partial postback from ASP.NET

F

fisindia.pruhawaii

Hi All,

I am facing an issue these days to execute Google Adwords tracking code during partial post back from ASP.NET.
Here below is the sample google tracking code which I want to be executed:

<!-- Google Code for execution -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = xxxxx;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "xxxxxxx";
var google_conversion_value = 0;
/* ]]> */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display: inline;">
<img height="1" width="1" style="border-style: none;" alt="" src="http://www.googleadservices.com/pag...0&amp;label=xxxxxxxx&amp;guid=ON&amp;script=0" />
</div>
</noscript>


Now the ScriptManager code through which I want this to be executed is :

ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "ScriptName", script, false);


Now it does not allow me to do following two things:
1. Won't allow me to add HTML style comments section like <!-- -->
2. The NoScript area is not permitted.

Can somebody help me how to get it through, may be not through ScriptManager and some other way.
Note: Doing full postback is not an option for me.

Thanks in advance
Amit Arora
 
B

Brian Cryer

Hi All,

I am facing an issue these days to execute Google Adwords tracking code
during partial post back from ASP.NET.
Here below is the sample google tracking code which I want to be executed:
Now the ScriptManager code through which I want this to be executed is :

ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "ScriptName",
script, false);


Now it does not allow me to do following two things:
1. Won't allow me to add HTML style comments section like <!-- -->
2. The NoScript area is not permitted.

Can somebody help me how to get it through, may be not through
ScriptManager and some other way.
Note: Doing full postback is not an option for me.

Thanks in advance

Simple, don't use ScriptManager. Instead use a PlaceHolder and add your
script to that.

So, if on your page you added:
<asp:placeHolder ID="AdwordsTrackingCode" runat="server">
</asp:placeHolder>

then in the code-behind you would use something like:

AdwordsTrackingCode.Controls.Add(new LiteralControl(script));

where script is a string containing your google code.

I think that should get round the issues you are having.
 
B

bradbury9

El jueves, 21 de junio de 2012 10:34:08 UTC+2, Brian Cryer escribió:
Simple, don't use ScriptManager. Instead use a PlaceHolder and add your
script to that.

So, if on your page you added:
<asp:placeHolder ID="AdwordsTrackingCode" runat="server">
</asp:placeHolder>

then in the code-behind you would use something like:

AdwordsTrackingCode.Controls.Add(new LiteralControl(script));

where script is a string containing your google code.

I think that should get round the issues you are having.

I have always found tricky inserting javascript and usually do take a similar approach (just i dont use placeholde, only a literalcontrol) but the approach is very similar, maybe a bit easier.

aspx
<updatepanel>
<literalcontrol />
</updatepanel>

aspx.cs
literalcontrol.value = script;
 
B

Brian Cryer

bradbury9 said:
El jueves, 21 de junio de 2012 10:34:08 UTC+2, Brian Cryer escribió:

I have always found tricky inserting javascript and usually do take a
similar approach (just i dont use
placeholde, only a literalcontrol) but the approach is very similar, maybe
a bit easier.

aspx
<updatepanel>
<literalcontrol />
</updatepanel>

aspx.cs
literalcontrol.value = script;

I use a placeholder out of habit, but if anything I'd say a literalcontrol
here is clearer and more intuitive.
So, I agree. Good observation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top