图像没有显示在gridview asp.net中

时间:2015-03-13 19:05:28

标签: c# asp.net gridview

我在网格的最后一列的每个单元格中插入一个 img 控件。但图像未显示(出现 X )。

我尝试在页面的其他位置放置一个html图像,以确保图像的路径正确,并在该html图像中,图片出现。检查页面的view source时,html图像和gridview中的图像都具有相同的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

1 个答案:

答案 0 :(得分:1)

在此代码中,图像距离当前目录一级,以及文件所在的位置。

您是否已调试,复制该路径,将其粘贴到另一个标签或窗口,并验证您没有收到404错误?听起来你的路径是关闭的,没有任何图像出现。

也许您需要使用"~/""/"语法从网站根目录进行映射?

修改

是的,你的道路是错的。试试&#34; / images&#34;或&#34;〜/ images&#34;它可能会起作用。 (假设在站点根目录)

您处于动态控件中 - 在ASP.NET控件上进行路径与HTML完全相同...