【问题标题】:WHy Gridview doesn't show image ?为什么 Gridview 不显示图像?
【发布时间】:2015-12-08 17:43:10
【问题描述】:

为什么在 Gridview 中没有加载图像?

<asp:GridView runat="server" ID="grdviewPropertyDetails" OnRowCommand="grdviewPropertyDetails_RowCommand" DataKeyNames="pk_PropertyDetails_PropertyDetailsID" AutoGenerateColumns="false" CssClass="table table-condensed table-bordered table-striped table-responsive">
   <Columns>    
     <asp:ImageField DataImageUrlField="PropertyDetailsFardPath" HeaderText="Fard Path" ControlStyle-Width="100" ControlStyle-Height = "100" />
     <asp:ButtonField CommandName="cmdEdit" ImageUrl="~/assets/global/images/shopping/edit.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
     <asp:ButtonField CommandName="cmdDelete" ImageUrl="~/assets/global/images/shopping/delete.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
   </Columns>
</asp:GridView>

返回的路径是这样的

G:\Study & Official Projects\Office Projects\Municipal Corporation\MCP-Building Plan Approval\MCP-Building Plan Approval\SiteImages\fard 2.png

是的,图像在其中,但 gridview 列是空的。

P.S 我像这样将它保存到数据库中:

if (FileUpload1.HasFile)
{
    string fileName = Path.GetFileName(FileUpload1.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/SiteImages/") + fileName);
    HdnFieldFard.Value = Server.MapPath(FileUpload1.FileName);
    ResultLabel.ResultLabelAttributes("Image Uploaded", ProjectUserControls.Enums.ResultLabel_Color.Red);
    ResultPanel.Controls.Add(ResultLabel);
}

string FardImagePath = HdnFieldFard.Value;

然后通过发送 FardImagePath 插入到数据库中

【问题讨论】:

  • 因为你只在数据库中存储文件名,所以如何在没有路径的情况下显示图像? HdnFieldFard 只包含文件名而不是完整路径。
  • 如何获取完整路径?
  • 你可以在db中存储fullpath + imagename。
  • 我将其存储在 DATABASE G:\Study & Official Projects\Office Projects\Municipal Corporation\MCP-Building Plan Approval\MCP-Building Plan Approval\SiteImages\fard 2.png
  • 如何在路径中添加“SiteImages”文件夹?

标签: c# asp.net visual-studio gridview webforms


【解决方案1】:
    Instead you can directly convert the BLOB or Binary in <asp:Image> tag and click on the Button Tag, it will take you to the IndexChangedFunction and you can do further operations with the grid.
Its perfectly worked for me, may be work for your requirement too:

        <asp:GridView ID="Gvid" runat="server" Width="100%" AutoGenerateColumns="false" OnSelectedIndexChanged="Gvid_SelectedIndexChanged" >
                <Columns>
    <asp:TemplateField HeaderText="IMAGE" Visible="false">
    <ItemTemplate>
    <asp:Image runat ="server"  ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("IMAGE")) %>' ID="Image"  />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:ButtonField DataTextField ="MODEL" CommandName="select" HeaderText ="MODEL" />
    </Columns> 
    </asp:GridView>

【讨论】:

    【解决方案2】:
    <asp:GridView runat="server" ID="grdviewPropertyDetails"     OnRowCommand="grdviewPropertyDetails_RowCommand" DataKeyNames="pk_PropertyDetails_PropertyDetailsID" AutoGenerateColumns="false" CssClass="table table-condensed table-bordered table-striped table-responsive">
      <Columns> 
    <asp:templateField>
    <itemTemplate>   
     <img src='<%#Eval("PropertyDetailsFardPath")%>' height="100px" width="100%"/>
         <asp:ButtonField CommandName="cmdEdit" ImageUrl="~/assets/global/images/shopping/edit.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
         <asp:ButtonField CommandName="cmdDelete" ImageUrl="~/assets/global/images/shopping/delete.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
    </itemTemplate>
    </asp:TemplateField>
       </Columns>
    </asp:GridView>
    

    【讨论】:

    • 让我试试,先生,请稍等
    • 路径保存成功但是IMAGE有问题,gridview现在只显示一个矩形框,没有图片,为什么?
    • 打开您的浏览器控制台并检查您的图像标签并检查 src ?正确与否。
    • 勾选,是这样的
    • 这个“PropertyDetailsFardPath”是正确的字段吗?跨度>
    【解决方案3】:

    而不是在数据库中保存完整路径。只需将图像名称保存在数据库中。在网格视图 rowdatabound 事件中,您可以更改图像 src。

    例如:Server.MapPath("~/SiteImages/") + 文件名

    【讨论】:

    • 我按照建议做了,但现在 grdivew 中只出现了矩形框
    • @Covert: 矩形框表示空白图片?
    • 做一件事:在这里写下你用代码写的东西。以及图像名称和文件夹名称。我向你展示它是如何工作的。
    • 此路径保存 G:\Study & Official Projects\Office Projects\Municipal Corporation\MCP-Building Plan Approval\MCP-Building Plan Approval\SiteImages\fard2.png
    • 我正在从 db PropertyDetailsFardPath 中选择这个字段
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    相关资源
    最近更新 更多