tags:

views:

358

answers:

2

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" />

Code Behind:

//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.

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

Any suggestions?

Thanks

+1  A: 

I think you might need to alter e.NewValues, not e.Keys. See the NewValues property on MSDN, it might point you in the right direction.

womp
A: 

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;
    }
}
JayTee