2015-03-13 109 views
1

我在我的網格的最後一列的每個單元格中插入一個img控件。但圖像沒有顯示(出現X)。圖像不顯示在GridView中asp.net

我試着把一個html圖像放在頁面的其他地方,以確保圖像的路徑是正確的,並在該html圖像中出現圖片。當檢查頁面的view source時,gridview中的html圖像和圖像都具有相同的ImageUrl

想知道這裏缺少什麼嗎?

圖像應在此插入:

<asp:BoundField DataField="RequestStatusId" HeaderText="Status" 
          ReadOnly="true" /> 

ASPX:

<asp:GridView ID="gvwRequests" runat="server" AutoGenerateColumns="False" 
        DataSourceID="odsRequests" AllowPaging="true" PageSize="10" 
        CssClass="GridStyle" onrowdatabound="gvwRequests_RowDataBound"> 
        <Columns> 
         <asp:ButtonField HeaderText="Request" DataTextField="Request" 
          SortExpression="Request" CommandName="OpenRequest" />        
         <asp:BoundField DataField="RequestDate" HeaderText="RequestDate" 
          SortExpression="RequestDate" /> 
         <asp:BoundField DataField="RequestNbr" HeaderText="RequestNbr" 
          SortExpression="RequestNbr" /> 
         <asp:BoundField DataField="PartNumber" HeaderText="PartNumber" 
          SortExpression="PartNumber" /> 
         <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" /> 
         <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> 
         <asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" /> 
         <asp:BoundField DataField="Customer" HeaderText="Customer" 
          SortExpression="Customer" />        
         <asp:BoundField DataField="RequestStatusId" HeaderText="Status" 
          ReadOnly="true" /> 
        </Columns> 
        <AlternatingRowStyle CssClass="GridAlternateRowStyle" /> 
        <RowStyle CssClass="GridRowStyle" /> 
        <PagerStyle CssClass="GridPagerStyle" /> 
       </asp:GridView> 

aspx.cs

protected void gvwRequests_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      Image img = new Image(); 
      string sColor = ListOfColorStatus.Find(item => item.RequestStatusId == Convert.ToInt32(e.Row.Cells[8].Text)).Color; 
      string sStatus = ListOfColorStatus.Find(item => item.RequestStatusId == Convert.ToInt32(e.Row.Cells[8].Text)).Status; 
      img.ImageUrl = "../images/" + sColor + "_ball.png"; 
      img.AlternateText = sColor; 
      img.ToolTip = sStatus; 
      e.Row.Cells[8].Controls.Add(img); 
     } 
    } 

來源:

<img src="../images/Yellow_ball.png" alt="Yellow" /> --> Appears 
       <div> 
    <table class="GridStyle" cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_gvwRequests" style="border-collapse:collapse;"> 
     <tr> 
      <th scope="col">Request</th><th scope="col">RequestDate</th><th scope="col">PONbr</th><th scope="col">PartNumber</th><th scope="col">Qty</th><th scope="col">Model</th><th scope="col">Brand</th><th scope="col">Customer</th><th scope="col">Status</th> 
     </tr><tr class="GridRowStyle"> 
      <td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$gvwRequests','OpenRequest$0')">2</a></td><td>03-12-2015</td><td>22222</td><td>32151458121</td><td>5</td><td>BLACKBERRY 8520</td><td>RESEARCH IN MOTION</td><td>CABLE &amp; WIRELESS ANTIGUA LTD</td><td><img title="Pending" src="../images/Yelow_ball.png" alt="Yelow" style="border-width:0px;" /></td> --> Does not appear 
+0

您可以將網站的目錄結構,所以我們可以看到這個頁面的相對路徑,圖片目錄 – CheGueVerra 2015-03-13 19:19:32

+0

@CheGueVerra請參閱更新 – Somebody 2015-03-13 19:30:10

+0

@有人看到我更新的答案... – Tim 2015-03-13 19:31:58

回答

1

在這段代碼中,圖像比當前目錄高一級,這就是文件的位置。

您是否調試過,複製該路徑,將其粘貼到另一個選項卡或窗口中,並驗證您沒有收到404錯誤?這聽起來像你的路徑是關閉的是沒有任何圖像顯示出來。

也許您需要使用"~/""/"語法來將其從站點根映射到它?

編輯

是啊,你的道路是錯誤的。嘗試「/圖像」或「〜/圖像」,它可能會工作。 (假設這是在站點根目錄)

你是在一個動態的控制 - 路徑功能上的ASP.NET控件不工作完全一樣的HTML ...