0

在代码中 在此代码中,我可以更新文件夹路径中的图像,但图像文件名因此无法保存在数据库中,我认为我的图像没有显示 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));
          }
     }
4

3 回答 3

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'is filename='<%# Bind("photo")%>'。当我们上传文件时,它的扩展名会自动保存。

于 2013-10-02T05:46:12.610 回答
0

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

于 2013-10-02T06:05:05.693 回答
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
          }
 }
于 2013-10-02T07:04:49.967 回答