R4R
Right Place For Right Person TM
 
R4R ASP.Net with C# ASP.NET Tutorials Upload Image
previous

Home

Next

How to upload Image in ASP .NET using C#

Step 1: Drag a image control and a button control. Click on Add Extender a small window open, select a popupControlExtender and click ok. The extender control be added.

Design Code (Default.aspx)

<%@ Page Language="C#" AutoEventWireup="true" 
Codevirtual="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit"
namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:Image ID="profile_image" runat="server" Width="142px" Height="193px"/>
       <br />
        <asp:Button ID="Button2" runat="server" Text="Edit Image" />
	<br />
        <table align="center"><tr><td><cc1:PopupControlExtender 
	ID="Button2_PopupControlExtender" runat="server" 
            DynamicServicePath="" Enabled="True" ExtenderControlID="" 
            PopupControlID="Panel1" TargetControlID="Button2">
        </cc1:PopupControlExtender>        
        <asp:Panel ID="Panel1" runat="server" BorderStyle="Solid" BorderWidth="1px" 
            BackColor="Gray" Height="120px" Width="227px"><br />
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Upload" 
	    onclick="Button1_Click" />
            <br />
        </asp:Panel></td></tr></table></div>
    </form></body></html>

Default.aspx.cs

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        profile_image.ImageUrl="~/Image/blank_img.jpeg";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    
        string filename = "";
        string path;
        if (FileUpload1.HasFile)
        {
            filename = FileUpload1.PostedFile.FileName;
            path=Server.MapPath("~/Image/") +FileUpload1.FileName;
            string onlyname = path.Substring(path.LastIndexOf("\\") + 1);
            profile_image.ImageUrl = "~/Image/" + onlyname;

        }
    }
}

 

Step 2: Run the Application and click on edit Button then click on Brower Button and select an image.

 

Then click on Upload Button. The profile image being uploaded

previous

Home

Next


Tolal:0 Click:
Show All Comments

Post Your Comments

Your Name:

Your Email ID :
Comments :
URL
  =* Enter SUM

Tutorials,Examples,Questions and Answer

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R