In order to use AJAX in ASP.NET we need to have a minimum of
ASP.NET 2.0 Framework and AJAX Extensions 1.0 for ASP.NET.
In order to upload a file using AJAX, we are in need of the
following main Web Server controls:
1.
FileUpload Server Control
2.
Button Server Control
Apart from the above, we need to specify the usual AJAX tags ScriptManager, UpdatePanel and ContentTemplate so that during run time the ASP.NET
AJAX will know which section of the web page needs to be updated. Let us take
a look at the code which uses the above:
Listing 1
1 <html>
2 <head>
3 <title>File Upload</title>
4 </head>
5 <body>
6 <form id="form1" runat="server">
7 <asp:ScriptManager ID="ScriptManager1" runat="server"/>
8 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
9 <ContentTemplate>
10 <asp:FileUpload ID="myFile" runat="server" />
11 <asp:Label ID="lblMsg" runat="server"></asp:Label>
12 <asp:Button ID="btnUpload" runat="server" Text="Upload">
13 </ContentTemplate>
14 </asp:UpdatePanel>
15 </form>
16 </body>
17 </html>
In the above code Line 7, 8 and 9 are the ones which specify
which part of this web page needs to be updated. The FileUpload control and the
Button control are within the AJAX UpdatePanel control.
You might think that when the user clicks the submit button,
the page will be submitted to the server. Well, this is not true. The above
code will not work. The reason is that FileUpload control does not work with
asynchronous post backs, and therefore does not work from within an AJAX
UpdatePanel. In simple words, the FileUpload control is not posted back to the
server.