简体   繁体   中英

setting javascript value from backend code

I would like to know how I can set a javascript value from my backend code. I have this line of JS code:

 gts.push([ 'google_base_offer_id', 'ITEM_PRODUCT_SEARCH_ID']);

What I am trying at this moment is:

gts.push(['google_base_offer_id', document.getElementById("hidden").value]);

I have hidden value set in the markup:

<asp:HiddenField ID="hidden" runat="server" />

and in the backend code, OnPreRender event, I am doing this:

hidden.Value = product.ProductId.ToString();

The value gets set properly but when I am viewing source in the browser the value is not populated. Am I doing something wrong, is it possible to populate the value like that? Thanks in advance, Laziale

您应该能够做到:

gts.push(['google_base_offer_id', document.getElementById('<%= hidden.ClientID %>').value]);

You could also try this way:

If you are using a ScriptManager in your page, then in the code behind you could register the javascript value in a hidden field

ScriptManager.RegisterHiddenField(this, "hiddenKey", "hiddenValue");

Then in aspx page you could use

gts.push(['google_base_offer_id', document.getElementById("hiddenKey").value]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM