ASP.NET GridView multiple files upload

Posted on February 19, 2008. Filed under: Uncategorized | Tags: , , , |

This article is about implementing ajax-like files upload in ASP.NET GridView control with each row in the grid having a upload control. After so many responses to my previous articles in making ajax-like file uploads using iframe, I now am writing onto an advanced requirement. The requirement is to add a file upload control in every row of a GridView control and have ajax-like implemenation (using iframes).

I won’t be writing code lines in my blog, as there is no good mechanism in the wordpress blog editor. Hence, I would publish screenshots gif; and I will put the downloadable files at the end.

Let me start with an example db table. Suppose I have a simple table for students and I am required to upload a profile picture for each student. I am also required to show if the student already has a picture uploaded, and disable the upload feature for them. So, here is the simple db structure that I am going to use with some sample data. The column names are self-explanatory.

GridView Upload - db

In the codebehind page, I am binding the GridView control (gvUploads) with a datareader in a subroutine named BindStudentsGrid().

GridView Upload - BindGrid

In the aspx page, I have put a panel with a GridView control and a button control inside it. Once the button is clicked, each GridView row should upload the files, one at a time. In GridView control, I have one templated column, which has an iframe. The source of the iframe is the page itself, and has two parameters passed from querystring: First is MODE=UPLOAD, which lets the page to render only the upload controls (described below), and the second one is ID to pass the primary key (StudentID). If the picture is already uploaded, the iframe is not displayed, but a ‘File Exists’ text is displayed. Also, note the id and name of the iframe has the format upfrm# – the hash is for the StudentID.

GridView Upload - grid template

In the same aspx page, I also have another panel named pnlUpload which displayed the file-upload control in each row of the GridView control, within the iframe that was created above. In this panel, I have a image (imgLoader, to display upload animation), a label (lblMessage, to display upload result), a file upload control, and a hidden button (btnUpload, that initiates the server postback).

GridView upload - upload panel template

To display pnlUpload only for the grid iframe, I have a function CheckIFrameMode() and have invoked it from Page_Load handler. As mentioned above, to find out which panel needs to be displayed, I am using querystring parameter named MODE.

GridView Upload - pageload check

Now, here is the javascript function, that implements the trick. The algorithm is to find all iframes in the grid that has file specified. You can now see why the name of the frame has upfrm# pattern. To make ajax-like upload, animation is displayed by the codes in line#36-37 below. The upload of the file is triggered by hidden button, btnUpload – the code for it is in line#40 below. This function is first invoked by btnStartUpload in pnlMain. The function only triggers one file upload. Next upload is triggered after the completion of the current file upload.

GridView upload - JS function

Finally, what I have is btnUpload click handler routine, which saves the uploaded file and displays appropriate messages. At the end, the js function is called again using RegisterStartupScript() function. The js function when invoked at the client side, processes next row in the grid.

GridView upload - btnUpload handler

And that is all. Here is the screenshot of the result page. File for row#1 and #5 is already there, so there is no upload. For row#2, the file was just uploaded from the page. File for row#3 is in upload process. Row#4 is pending. Row#6 and #7 does not have file specified, so they won’t be processed.

GridView upload - screenshot

For applications like DotNetNuke or something with master pages, the generation of only the upload form (pnlUpload) is not easily possible. You can generate the form using Response.Write() (and ending the response with Response.End()) or you can use a different aspx page for the upload control section.

download the code

Read Full Post | Make a Comment ( 6 so far )

ajax extensions file upload workaround

Posted on March 31, 2007. Filed under: Uncategorized | Tags: , , , |

In my previous post of “Upload files using ajax extensions,” I had written my approach that I implemented to upload files. As about 65% of the total clicks to my blog page is only to that post, I have decided to write a new post with complete codes for file upload. The code is fully functional and is tested in Firefox 2 and IE6.

The approach is much similar to my previous post, as it uses iframes. But in this project, I have created everything in a single file, so all codes can be handled easily. And coder will feel that s/he is completely implementing ajax extensions, in addition to users who will have experience of partial page postback.

In this simple project, I have written code to do file upload and display all files uploaded in a listbox without doing full page postback. ajax extension - file upload project




download project file



NEW: Uploading files in a GridView (Feb 20, 2008).



Read Full Post | Make a Comment ( 48 so far )

Uploading files using ajax extensions

Posted on March 13, 2007. Filed under: Uncategorized | Tags: , , , |

The current ajax extension (Version: 1.0.61025.0) does not support file upload within the UpdatePanel. I am not so sure why, but I have heard it to be because browser security limitations. As browsers do not allow direct access to user file system via javascript codes, and due to this reason the <input type=”file”> cannot be modified by javascripts and also the details of file cannot be accessed without any activex, not working of file uploads in ajax approach is quite obvious.

But, most of us all have seen the new generation internet mailbox system, which does upload files (attachments) in background without page refresh. The method then should be ajax implementation or some other trick. I have no idea!

When GMail was in baby stage, I had heard that it uses hidden frames to implement ajax like infrastructure – due to browser security reasons. (But I think, GMail now uses ajax). Knowing this rumor, I once happened to create multiple files upload system using hidden iframe and javascripts – user would provide a list of files and they would get uploaded to the server one by one. The creation easily solved the problem of maximum file size limit, and also errors when doing large file upload.

Now, as I am onto ajax extensions and as the uploading is not working within UpdatePanel, I went to give the same approach a try. And after some good amount of time, I was able to do it. Here is my approach outline in basic form:

1. I first created an upload form in separate page which is like this (myIframe.html):
<form method="POST" target="_self" enctype="multipart/form-data" action="main.aspx">
<input type="file" name="fileUpload" />
<input type="submit" value="UPLOAD" />

2. In my main page (main.aspx), I put an iframe and loaded that page in it. I also made borders invisible so it did not look like it is an iframe.
<iframe name="iUploadFrame" src="myIframe.html" frameborder="0" onload="iUploadFrameLoad();"></iframe>

Note that I am also specifying onload event handler function of javascript. I will explain it below.

3. For testing, in main.aspx I also have UpdatePanel with a button and label like this:
<asp:UpdatePanel ID="pnlMain" runat="server">
<asp:Label id="lblMessage" runat="server" />
<asp:Button id="btnUploadComplted" runat="server" style="visibility:hidden;" />
<asp:AsyncPostBackTrigger ControlID="btnUploadCompleted" EventName="Click" />

Note that I have hide the button using CSS style, but not setting visible=”False” attribute. This is because I still want to have the button html in the output document and would want to trigger the ajax callback via that button.

3. I then define the click event hander of the button in the codebehind as:
Protected Sub btnUploadCompleted_Click(s As Object, e As EventArgs)
End Sub

4. Now, to save file I write following code in the code-behind:
Protected Sub Page_Load(s As Object, e As EventArgs)
If Request.Files.Count = 1 Then
' process to save file
End If
End Sub

5. Now all that we need to do is create javascript function to integrate the process into ajax implementation. The event will trigger when upload process is complete, and it will, in turn, triggers btnUploadCompleted’s click event, which is responsible for making our ajax call.
<script type="text/javascript">
function iUploadFrameLoad() {
if (window.iUploadFrame.document.body.innerHTML == "SUCCESS") {



NEW: I have written a new post with complete source code for this process. View post.



NEW: Uploading files in a GridView (Feb 20, 2008).



Do write me comments and suggestions on this article.

Read Full Post | Make a Comment ( 32 so far )

Liked it here?
Why not try sites on the blogroll...