Вопрос

I have a Webshop. On each page load it needs to load some scripts, mostly Google Analytics, according to the users current step (step 1, step 2 etc.).

The scripts are stored in a database where they are received with the current step as the key. Some of the scripts shall go to the end of the section, and some of the shall go to the end of - this information is also stored in the database.

I have tried with registerClientScript, but it doesn't let me inject into specific areas of the page.

I use a MasterPage.

Это было полезно?

Решение

One option is to use a Literal in each place where you need to inject JavaScript. In code-behind, on each page load clear the content of each literal, and then check the step of the user and re-inject the required JS. This keeps literals clean and avoids JS code persisting across postbacks where it might not be needed.

Another option depending on how many JS scripts you have is to put each one in a placeholder, then show/hide that placeholder as required.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top