Я думаю, что вы лучше всего решите эту проблему, перезвонив на сервер (через AJAX), чтобы получить рекламу после отображения страницы в браузере конечного пользователя.
Это можно сделать с помощью нескольких технологий (AJAX.NET и UpdatePanels, обычный старый Javascript или JS-фреймворк, например, jQuery или MooTools, а также веб-службы для показа рекламы), в зависимости от вашего удобства. *
С опцией jQuery + ASHX вы можете сделать следующее:
В JavaScript:
// when the document has finished loading
$(document).load(function() {
// make an AJAX request to MyHandler.ashx, with the content's height
var height = $("#ContentContainer").height()
$.get("MyHandler.ashx?contentheight=" + height, ResponseCallback);
}
// put the server's response (data) into the ad container
function ResponseCallback(data) {
$("#AdContainer").html(data);
}
В HTML:
<body>
<div id="ContentContainer">
...
...
</div>
<div id="AdContainer"></div>
</body>
MyHandler.ashx:
public void ProcessRequest(HttpContext context) {
HttpRequest request = context.Request;
HttpResponse response = context.Response;
int height = Convert.ToInt32(request.QueryString["contentheight"] ?? "0");
// do something to calculate number of ads and get the HTML for the ads
// assuming we have a list of Advert objects:
List<Advert> ads = GetSomeAds(height);
foreach(Advert a in ads) {
response.Write(a.GetHtml());
}
}
Очевидно, что наиболее интегрированной с ASP.NET является опция UpdatePanel, хотя я бы порекомендовал вам перейти к использованию JS-инфраструктуры с .ASHX (пользовательские обработчики) или .ASMX (веб-службы) на стороне сервера. Это намного более прозрачно и понятно с точки зрения знания «что делает этот код?». UpdatePanels может показаться черной магией.