如何在网格视图中显示图像
本文关键字:显示 显示图 图像 视图 网格 | 更新日期: 2023-09-27 18:33:43
>这里在代码中 在此代码中,我可以更新文件夹路径中的图像,但由于这个原因,图像文件名无法保存在数据库中,我认为我的图像没有显示网格视图请帮助我
<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));
}
}
您的问题在于数据绑定。由于我们只有几行代码,因此无法找出实际问题。但据我所知,绑定达不到标记。假设 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
}
}
在上传按钮单击事件中尝试此代码。
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")%>'
扩展。当我们上传文件时,其扩展名会自动保存。
要
上传图像,请使用@Aayushi Jain解释的代码。 要将图像绑定到网格视图,您需要使用 ResolveUrl
.为此,您需要在网格视图中获取ImageField
。