FormView + FileUpload-我可以基于fileupload更改绑定字段吗? [英] FormView + FileUpload - can I change a bound field based on the fileupload?
问题描述
我在ASP.NET 2.0应用程序中有一个FormView控件.我已经在数据库中将文件名(一个人的照片)存储在一列中.我无法将列的值绑定到fileupload控件-因此,我尝试使用隐藏的表单字段.这就是我所拥有的:
I have a FormView control in an ASP.NET 2.0 app. I've got the database storing a filename (a person's picture) in a column. I can't bind the value of the column to a fileupload control - so I'm trying to use a hidden form field. Here's what I have:
<asp:HiddenField ID="pictureLink" runat="server" Value='<%# Bind("pictureLink") %>' />
<asp:FileUpload ID="pic" runat="server" />
后面的代码:
//ItemUpdating event handler
void do_update(object sender, FormViewUpdateEventArgs e)
{
FileUpload newpic = (FileUpload)profile_edit.FindControl("pic");
if (newpic.HasFile)
{
//do a bunch of file uploading "stuff" which makes a new file name
e.Keys["pictureLink"] = new_filename;
}
}
我的目标是将隐藏表单字段的值更新为新更新的文件名,以便正确地更新数据库.
My goal is to update the hidden form field's value to the newly updated file name so the database is properly updated.
我认为我已经接近了-但是看来我不能在事后以编程方式更改任何绑定的数据字段.
I think I'm close - but it seems like I can't programmatically alter any of the bound data fields after-the-fact.
我尝试使用javascript更改控件-但新文件名实际上与他们上传的文件名不同;哪种javascript不一定能预测"并可靠地将正确的文件名放入隐藏的表单字段中
I've tried using javascript to change the control - but the new file name will actually be different than what they upload; which javascript can't necessarily "predict" and reliably put the correct file name into the hidden form field
有什么建议吗?
谢谢
推荐答案
好-我在发布问题后不久就找到了答案.如果有人有更好(更优雅)的解决方案,我将其保持打开状态.基本上,我更改了do_update事件处理程序以拦截文件上传.如果有文件,则我编辑NewValues集合,以便数据库接收新文件名,而不是旧文件名.
OK - I found the answer not too long after I posted the question. I'll leave it open in case someone has a better (more elegant) solution. Basically, I change the do_update event handler to intercept the file upload. If there's a file, then I edit the NewValues collection so that the database receives the new file name instead of the old one.
//ItemUpdating event handler
void do_update(object sender, FormViewUpdateEventArgs e)
{
FileUpload newpic = (FileUpload)profile_edit.FindControl("pic");
if (newpic.HasFile)
{
//do a bunch of file uploading "stuff" which makes a new file name
//HERE IS THE CHANGE - update the newvalues object to the new file name
e.NewValues[1] = new_filename;
}
}
这篇关于FormView + FileUpload-我可以基于fileupload更改绑定字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!