簡體   English   中英

在中繼器中設置背景圖像

[英]Setting background image in a repeater

我試圖以編程方式在中繼器中設置背景圖像。 以下內容的變體沒有用,包括嘗試在div設置url,而不是jquery函數:

JQuery函數:

var getBackgroundImage = function (imagePath) {
    var backgroundImage = 'url(' + iipServ + '?FIF=/IIPServer/images/' + imagePath + '&WID=40&CVT=PNG' + ')';
    return backgroundImage;
};

ASP頁面:

 <asp:Repeater ID="docResults" runat="server" ItemType="ArchiveViewer.Models.Document" 
     SelectMethod="GetSearchResults" >
     <ItemTemplate> 
         <div class="result" data-docid="<%#:Item.DocumentId %>" 
             data-imageDir="<%#:Item.FolderPath %>" 
             data-objData="<%#:Item.JSONPath %>" 
             style="<%= getBackgroundImage(Item.Pages.First().ImagePath) %> ">  
                  <%#:Item.Metadata.Title %>
         </div>

     </ItemTemplate>
 </asp:Repeater>

能做到嗎? 怎么樣? 謝謝!

編輯:每個div都有自己的圖像。 我從服務器獲取URL。

編輯2:我不是使用jquery函數,而是在后面的代碼中使用web方法:

[WebMethod]
    public string getBackgroundImage(string path)
    {
        string iipServer = ConfigurationManager.ConnectionStrings["iipServer"].ConnectionString;
        string urlString = "background-image : url('" + iipServer + "?FIF=/IIPServer/images/" + 
                path + "&WID=40&CVT=PNG)'";
        System.Diagnostics.Debug.WriteLine(urlString);
        return urlString;
    }

在ASPX頁中:

style="background-image : <%=getBackgroundImage(Item.Pages.First().ImagePath) %>">

在后面的代碼中:

public string getBackgroundImage(string iipServ, string path)
{
    return "url('" + iipServ + "?FIF=/IIPServer/images/" + path + "&WID=40&CVT=PNG)'";
}

目前尚不清楚什么是iipServ ,以及從何處獲取它。 只需沿路徑將其傳遞給C#函數即可。

您應該刪除JS函數,這將全部在服務器端進行。

pid:您的答案幾乎是正確的,但是編譯器不喜歡它。 不過,它幫助我弄清楚了如何正確地進行操作。 謝謝。

萬一它可以幫助別人,這是我解決此問題的方法:

我的ASPX頁面:

<ItemTemplate> 
      <div class="result" data-docid="<%#:Item.DocumentId %>" 
          data-imageDir="<%#:Item.FolderPath %>" data-objData="<%#:Item.JSONPath %>"
          style="<%#: getBackgroundImage(Item.Pages.First().ImagePath) %>" >
        <%#:Item.Metadata.Title %>
      </div>
</ItemTemplate>

我的代碼后面:

    [WebMethod]
    public string getBackgroundImage(string path)
    {
        string iipServer = ConfigurationManager.ConnectionStrings["iipServer"].ConnectionString;
        string urlString = @"background-image:url(" 
                + iipServer + "?FIF=/IIPServer/images/" + 
                path + "&WID=40&CVT=PNG)";
        return urlString;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM