如何在gridview中显示图像?

时间:2013-10-02 05:33:32

标签: c# asp.net

这里的代码在这段代码中,我可以更新文件夹路径中的图像,但图像文件名因此无法保存在数据库中,我认为我的图像不显示gridview 请帮助我

<EditItemTemplate>
<asp:FileUpload ID="photoTextBox" runat="server" filename='<%# Bind("photo")%>.jpg' />                                   
</EditItemTemplate>

<ItemTemplate>    
<img alt="" src="ImageStorage/<%# Eval("personalid") %>.jpg" width="40" height="40" id="image1" />
<asp:Label ID="photoLabel" runat="server" Text='<%# String.Concat(Eval("personalid"),".jpg")%>' ></asp:Label>                                 
</ItemTemplate>

C#

 protected void RadGrid4_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            GridEditableItem eitem = e.Item as GridEditableItem;
            FileUpload photoTextBox = eitem.FindControl("photoTextBox") as FileUpload;
            TextBox personalidTextBox = eitem.FindControl("personalidTextBox") as TextBox;
            Label photoLabel = eitem.FindControl("photoLabel") as Label;

            string filename = Path.GetFileName(personalidTextBox.Text + ".jpg");           

            photoTextBox.SaveAs(Server.MapPath("ImageStorage/" + filename));
          }
     }

3 个答案:

答案 0 :(得分:0)

在上传按钮点击事件中尝试此代码。

protected void UploadButton_Click(object sender, EventArgs e)
  {
   // Specify the path on the server to
   // save the uploaded file to.
   String savePath = @"c:\temp\uploads\";

   if (FileUpload1.HasFile)
   {
     String fileName = FileUpload1.FileName;
     savePath += fileName;
     FileUpload1.SaveAs(savePath);
   }
  }

同时从filename='<%# Bind("photo")%>.jpg'移除filename='<%# Bind("photo")%>'的扩展程序。当我们上传文件时,会自动保存其扩展名。

答案 1 :(得分:0)

上传图片使用@Aayushi Jain所解释的代码。要将该图像绑定到网格视图,您需要使用ResolveUrl。为此,您需要在gridview中使用ImageField

答案 2 :(得分:0)

您的问题在于数据绑定。因为我们只有几行代码所以无法弄清楚实际问题。但据我所知,绑定不符合标记。假设ImagePath是db列,请尝试按以下方式更改aspx:

ASPX

<EditItemTemplate>
    <asp:FileUpload 
         ID="photoTextBox" 
         runat="server" />                                  
</EditItemTemplate>

<ItemTemplate>    
<img alt="" 
     src='<%#Eval("ImagePath")%>' 
     width="40" 
     height="40" 
     id="image1" />

<asp:Label 
     ID="photoLabel" 
     runat="server" 
     Text='<%#String.Concat(Eval("personalid"),".jpg")%>' >
</asp:Label>

代码背后:

protected void RadGrid4_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            GridEditableItem eitem = e.Item as GridEditableItem;
            FileUpload photoTextBox = eitem.FindControl("photoTextBox") as FileUpload;
            TextBox personalidTextBox = eitem.FindControl("personalidTextBox") as TextBox;
            Label photoLabel = eitem.FindControl("photoLabel") as Label;

            string filename = String.Format("{0}.jpg",personalidTextBox.Text);           
            string uploadPath = string.Format("~/ImageStorage/{0}",fileName)
            photoTextBox.SaveAs(Server.MapPath(uploadPath));
            ViewState["UploadPath"]=uploadPath;
            // use the above viewstate path to save in db in gridview rowupdating event and bind the grid again
          }
 }