我应该使用什么代替 e.Values.add(... 用于 ListViewEditEventArgs 上传图像
当引用 ListViewInsertEventArgs 的 e.Values 方法时,我可以使用 linq 和 listview 控件将图像上传到数据库,但 ListViewEditEventArgs 中没有这样的方法,那么我可以使用什么来实现相同的结果?
这是我的插入代码:
protected void ProjectPhotosList_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload uplImage = (FileUpload)ProjectPhotosList.InsertItem.FindControl("uplImage");
标签 fileuploadlbl = (Label)ProjectPhotosList.InsertItem.FindControl("fileuploadlbl");
byte[] img = null;
if (uplImage.HasFile || !uplImage.FileName.ToLower().EndsWith(".jpg"))
{
try
{
img = new byte[uplImage.PostedFile.ContentLength];
uplImage.PostedFile.InputStream.Read(img, 0, img.Length);
}
catch
{
fileuploadlbl.Text = "unable to upload " + uplImage.FileName.ToString();
}
}
if (img == null)
{
e.Cancel = true;
fileuploadlbl.Text = "Please choose a file to upload";
}
try
{
e.Values.Add("ProjectPhoto", new System.Data.Linq.Binary(img));
fileuploadlbl.Text = "File Upload Successful";
}
catch
{
fileuploadlbl.Text = "File Upload Failed, please try again";
}
}
i can upload images to the database using linq and the listview control when referancing the e.Values method for the ListViewInsertEventArgs, but there is no such method in the ListViewEditEventArgs, so what can i use to achieve the same results?
here is my inserting code:
protected void ProjectPhotosList_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload uplImage = (FileUpload)ProjectPhotosList.InsertItem.FindControl("uplImage");
Label fileuploadlbl = (Label)ProjectPhotosList.InsertItem.FindControl("fileuploadlbl");
byte[] img = null;
if (uplImage.HasFile || !uplImage.FileName.ToLower().EndsWith(".jpg"))
{
try
{
img = new byte[uplImage.PostedFile.ContentLength];
uplImage.PostedFile.InputStream.Read(img, 0, img.Length);
}
catch
{
fileuploadlbl.Text = "unable to upload " + uplImage.FileName.ToString();
}
}
if (img == null)
{
e.Cancel = true;
fileuploadlbl.Text = "Please choose a file to upload";
}
try
{
e.Values.Add("ProjectPhoto", new System.Data.Linq.Binary(img));
fileuploadlbl.Text = "File Upload Successful";
}
catch
{
fileuploadlbl.Text = "File Upload Failed, please try again";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我已经解决了这个问题! 我只需要采取一些不同的方式:
这是重要的代码:
int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString());
它只是获取所选行的主键值的简单方法。
我发现了一篇关于将 pdf 上传到数据库的帖子,并决定将我的其余代码基于该帖子。 所以这里是完整的代码:
protected void ProjectPhotosList_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
FileUpload myFile = (FileUpload)ProjectPhotosList.EditItem.FindControl("uploadImage");
ok so i have solved the issue! I just had to go about it a bit of a different way:
this is the important code:
int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString());
its just a simple way to get the primarykey value of the selected row.
I found a post about uploading pdf's to a database and decided to base the rest of my code on that. So here the full code:
protected void ProjectPhotosList_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
FileUpload myFile = (FileUpload)ProjectPhotosList.EditItem.FindControl("uploadImage");