How to fileuplode control in update panel using asp.net with c#


How to fileuplode control in update panel using asp.net with c#

In this article, I will explain with example how to upload Image/ file through File Upload Control that is placed inside Update Panel in asp.net Ajax using both C#.


  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuplode .aspx.cs" Inherits="fileuplode " %&gt;

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Upload file example in asp.net</title>
    </head>
    <body>
        <form id="form1" runat="server">
               <asp:scriptmanager id="ScriptManager1" runat="server">
            </asp:scriptmanager>
        
            <asp:updatepanel id="UpdatePanel1" runat="server">
            <contenttemplate>
                <asp:fileupload id="FileUpload1" runat="server">
                <asp:button id="btnUpload" onclick="btnUpload_Click" runat="server" text="Upload"><br />         
                <asp:image id="imgShow" runat="server" width="150px">
            </asp:image></asp:button></asp:fileupload></contenttemplate>
           <triggers>
           <asp:postbacktrigger controlid="btnUpload">
           </asp:postbacktrigger></triggers>
            </asp:updatepanel>
        </form>
    </body>
    </html>



Source Code C#


  1. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

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

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string ImgPath = Server.MapPath("~/Images/" + Guid.NewGuid() + FileUpload1.FileName);
                FileUpload1.SaveAs(ImgPath);
                string ShowImgPath = ImgPath.Substring(ImgPath.LastIndexOf("\\"));
                imgShow.ImageUrl = "~/Images" + ShowImgPath;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Please select the image to upload');", true);
            }
        }

    }




Post a Comment

1 Comments