【问题标题】:ASP.NET Adrotator Control Repeating For Length of Entire PageASP.NET Adrotator 控件重复整个页面的长度
【发布时间】:2011-02-19 18:31:34
【问题描述】:

我正在制作一个网站,该网站需要在页面长度上以动态长度重复广告。我希望广告在页面的整个长度上显示,但在数据显示之前我不会知道该长度。 .NET 中是否有内置功能?如果没有,是否有人看到我可以采用的任何解决方法来为我做这件事?

谢谢!

【问题讨论】:

    标签: asp.net adrotator


    【解决方案1】:

    我认为您最好通过在最终用户的浏览器上呈现页面后回调服务器(通过 AJAX)来获取广告来解决此问题。

    您可以通过多种技术(AJAX.NET 和 UpdatePanels、plain-old-Javascript 或 jQuery 或 MooTools 等 JS 框架以及提供广告的 Web 服务)来做到这一点,具体取决于您的舒适度。

    使用 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 选项,尽管我建议您在服务器端使用带有 .ASHX(自定义处理程序)或 .ASMX(Web 服务)的 JS 框架。就知道“这段代码在做什么?”而言,它更加透明和易于理解。 UpdatePanel 看起来像是黑魔法。

    【讨论】:

    • 非常感谢。我做了类似的事情来解决这个问题,但这要优雅得多。
    猜你喜欢
    • 2012-08-31
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2012-04-12
    相关资源
    最近更新 更多