SIGN UP MEMBER LOGIN:    
ARTICLE

Upload File With Update Panel

Posted by Rahul Kumar Saxena Articles | Learn .NET October 29, 2009
In this I am going to show how we will upload file with update panel in asp.net.
Reader Level:

I posted in my previous article how we can upload file. But I face problem when I try to upload file with update panel. The File Upload Control doesn't work inside an Update Panel due to security reasons and restrictions a browser implies. But by using some property we can upload a file with update panel.

In this article I am going to show how we can upload file in asp.net with update panel. Here I use

<Triggers>

    <asp:PostBackTrigger ControlID="ButtonUpload" />

</Triggers>

 

This is the aspx code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Upload File With Update Panel</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <table cellpadding="4" cellspacing="4" width="90%" align="center">

            <tr>

                <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                    <ContentTemplate>

                        <asp:Label ID="Label1" runat="server" Text="Select A File To Upload:" ForeColor="Black"></asp:Label>

                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                        <asp:FileUpload ID="FileUpload1" runat="server" />

                        <asp:Button ID="ButtonUpload" runat="server" Height="25px" OnClick="ButtonUpload_Click" Text="Upload"

                            Width="128px" />

                        <br />

                        <asp:Label ID="Label2" runat="server"></asp:Label>

                        <br />

                    </ContentTemplate>

                    <Triggers>

                        <asp:PostBackTrigger ControlID="ButtonUpload" />

                    </Triggers>

                </asp:UpdatePanel>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

 

This is the aspx.cs code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void ButtonUpload_Click(object sender, EventArgs e)

    {

         FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));

        Label2.Text = FileUpload1.PostedFile.FileName;

        Label2.Text = FileUpload1.PostedFile.FileName;

    }

}

 

When I run the application then output

UploadFileWithUpdatePanel.JPG


Image 1.


Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor