2013-10-02 142 views
0

這裏的代碼在這段代碼中,我可以更新的文件夾路徑,但圖像文件名的圖像無法在數據庫中保存這個原因,我認爲我的形象是不顯示的GridView plz幫助我我如何在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)); 
      } 
    } 
+0

您是否嘗試過使用訪問圖像之一「網址/ ImageStorage /{sampleid}.jpg「在瀏覽器中?如果它顯示圖像,請檢查客戶端生成的HTML源,以查看圖像src是否正確生成..? – Afshin

回答

0

您的問題與數據綁定有關。由於我們只有幾行代碼,所以無法找出實際問題。但據我所見,裝訂不符合標記。假設的ImagePath是數據庫列嘗試改變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 
      } 
} 
+0

它告訴我錯誤無法映射路徑'/ImageStorage/395.jpg'。 –

+0

現在IMG已更新,但它不存儲在數據庫... PLZ幫助 –

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")%>'刪除的擴展。當我們上傳文件時,其擴展名會自動保存。

0

上傳圖片使用@Aayushi Jain解釋的代碼。並將該圖像綁定到網格視圖,則需要使用ResolveUrl。爲此,您需要在gridview中使用ImageField