为什么Gridview没有显示图像?

时间:2015-09-12 06:03:19

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

为什么Image没有加载到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我将它保存到DB中:

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

插入到db中

3 个答案:

答案 0 :(得分:1)

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

Ex:Server.MapPath(&#34;〜/ SiteImages /&#34;)+ fileName

答案 1 :(得分:0)

<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>

答案 2 :(得分:0)

    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>
相关问题