Skip to content

ASP.Net Listview with FileUpload Control & LinqDataSource

March 19, 2009

So this was a little harder than I thought it would be.  I’m still a little shocked that it wasn’t a more common question.  So here is what we’re going for, a ListView databound to a LinqDatasource which has a column with an image path.  We want to show that image in the grid and allow for uploading new images to new and existing rows. Like this:


And the insert


The source for the above would look like this:


See that we’re just wrapping the image in a link and shrinking it down. 


Again, no biggie, just a regular FileUpload control.

Now lets look at the real magic thats going to make this work.  The first thing we need is a way to find the control from our code-behind.  This is pilfered from everyone’s friend Jeff Atwood from over at Coding Horror.


Then we’re going to going to override the LINQ inserting and inserted events:


This should be pretty straightforward.  We grab a handle to the FileUpload object and create the server path (I know, it should be inside the IF but I don’t want to redo the screenshot)  Then we save the file to the server.


Now after the row has been inserted we’ll check that FileUpload control again, get the row that was just inserted, set the path and update the row just in time for the postback.


You could probably wrap the whole thing up in an UpdatePanel and make it all slick and Ajaxy but I think what we’ve got here is pretty neat as is.  Enjoy.

2 Comments leave one →
  1. September 11, 2009 6:45 pm

    Thanks for posting this. I’ve just spent most of the day trying to figure out how to do this. (And I would never have come up with this solution in a million years.)

  2. Russ permalink
    April 16, 2013 6:38 pm

    Thanks a million. Love the integration of the FindControlRecursive, that part was killing me until i found a link to this article on Stackoverflow. Thanks!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: