图像不显示在GridView中asp.net
问题描述:
我在我的网格的最后一列的每个单元格中插入一个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 & WIRELESS ANTIGUA LTD</td><td><img title="Pending" src="../images/Yelow_ball.png" alt="Yelow" style="border-width:0px;" /></td> --> Does not appear
答
在这段代码中,图像比当前目录高一级,这就是文件的位置。
您是否调试过,复制该路径,将其粘贴到另一个选项卡或窗口中,并验证您没有收到404错误?这听起来像你的路径是关闭的是没有任何图像显示出来。
也许您需要使用"~/"
或"/"
语法来将其从站点根映射到它?
编辑
是啊,你的道路是错误的。尝试“/图像”或“〜/图像”,它可能会工作。 (假设这是在站点根目录)
你是在一个动态的控制 - 路径功能上的ASP.NET控件不工作完全一样的HTML ...
您可以将网站的目录结构,所以我们可以看到这个页面的相对路径,图片目录 – CheGueVerra 2015-03-13 19:19:32
@CheGueVerra请参阅更新 – Somebody 2015-03-13 19:30:10
@有人看到我更新的答案... – Tim 2015-03-13 19:31:58