Thursday, 12 June 2014

How To Use FileUpload Control in Update Panel For Ajax File Upload In Asp.Net Using C#

6/12/2014 - By Pranav Singh 0

In this article I will show you how to use fileupload Control in update panel for ajax file upload In asp.Net   Uusing C#.Net and VB.net.



Now for this article first we will create a new asp.net application and add fileupload control and button control. After adding all the controls your .aspx page code will look as shown below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadByUpdatePanel.aspx.cs"
    Inherits="ProjectDemo_Asp.et.FileUploadByUpdatePanel" %>

<!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>How To Use FileUpload Control in Update Panel For Ajax File Upload In Asp.Net
        Using C#</title>
         <script type="text/javascript">
             function ShowProgress() {
                 document.getElementById('<% Response.Write(UpdateProgress1.ClientID); %>').style.display = "inline";
             }
    </script>   
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
        <ProgressTemplate>
            Please wait....
        </ProgressTemplate>
    </asp:UpdateProgress>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            &nbsp;<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" OnClientClick="ShowProgress();"/>
            <br />
            <asp:Label ID="lblmessage" runat="server" Text=""></asp:Label>
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="Button1" runat="server"/>
        </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>

In above code we have used JavaScript code this code is used for showing the please wait message till the file processing not get completed.

         <script type="text/javascript">
             function ShowProgress() {
                 document.getElementById('<% Response.Write(UpdateProgress1.ClientID); %>').style.display = "inline";
             }
    </script>   

Now in your .cs page we will add the below code.

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectDemo_Asp.et
{
    public partial class GetUpoadedFileDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           if (FileUpload1.HasFile)
            {
                string filenanmme = System.IO.Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/Images/" + filenanmme));
                lblmessage.Text = "File uploaded successfully.";
            }
            else
            {
             lblmessage.Text = "Please select file.";
            }
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ProjectDemo_Asp.et
    Partial Public Class GetUpoadedFileDetail
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            If FileUpload1.HasFile Then
                Dim filenanmme As String = System.IO.Path.GetFileName(FileUpload1.FileName)
                FileUpload1.SaveAs(Server.MapPath("~/Images/" + filenanmme))
                lblmessage.Text = "File uploaded successfully."
            Else
                lblmessage.Text = "Please select file."
            End If
        End Sub
    End Class
End Namespace

In above code we will be able use file upload control in update panel in your asp.net application.
Now run the page to get the output.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top