How to use Asp.net
  • Home
  • About us
  • Disclaimer
  • joy of helping
KEEP IN TOUCH

How to display image in Image control after upload on the server asp.net C#

Mar27
2011
5 Comments Written by vikram

I have posted an article where we can create a folder dynamically and can upload the images in it and display the image name in ListBox. Now in this article we do the same thing only one thing is new that we will display the selected image in Image Control.

*You can use the code with database according to your need.

*Set the AutoPostBack=true of the ListBox property

Here following the source code.

*you can copy paste it in your source code.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”dynamicfolder.aspx.cs” Inherits=”dynamicfolder” %>

<!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>

<style type=”text/css”>

.style1

{

width: 100%;

}

</style>

</head>

<body>

<form id=”form1″ runat=”server”>

<table class=”style1″>

<tr>

<td>

Enter Folder name

</td>

<td>

<asp:TextBox ID=”txt_foldername” runat=”server”></asp:TextBox>

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Button ID=”btn_submit” runat=”server” Text=”Create Folder”

onclick=”btn_submit_Click” />

</td>

</tr>

<tr>

<td>

Select Image</td>

<td>

<asp:FileUpload ID=”FileUpload1″ runat=”server” />

<asp:RegularExpressionValidator ID=”RegularExpressionValidator1″

runat=”server”

ErrorMessage=”Invalid File!(only  .gif, .jpg, .jpeg, .wav Files are supported)”

ValidationExpression=”^.+(.jpg|.JPG|.gif|.GIF|.jpeg|JPEG)$”

ControlToValidate=”FileUpload1″></asp:RegularExpressionValidator>

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Button ID=”btn_fileupload” runat=”server” Text=”Upload Image”

onclick=”btn_fileupload_Click” />

&nbsp;&nbsp;&nbsp;

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:ListBox ID=”ListBox1″ runat=”server” AutoPostBack=”True” Height=”105px”

onselectedindexchanged=”ListBox1_SelectedIndexChanged” Width=”109px”>

</asp:ListBox>

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Image ID=”Image1″ runat=”server” Height=”156px” Width=”202px” />

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click”

Text=”Delete Selected Image” />

</td>

</tr>

</table>

<div>

</div>

</form>

</body>

</html>


 

Following the code

 

using System;

using System.Collections;

using System.Configuration;

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;

using System.IO;

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

{

String fn;

Int32 cnt;

Int32 i;

String pth;

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btn_submit_Click(object sender, EventArgs e)

{

// creating folder dynamically on server

if (Directory.Exists(Server.MapPath(txt_foldername.Text)))

return; // if allready exists then it will not create it

else

// if not created then it will create it.

Directory.CreateDirectory(Server.MapPath(txt_foldername.Text));

}

protected void btn_fileupload_Click(object sender, EventArgs e)

{

String save; // storing the path of the image filename

// if image file size is greater then below condition will execute it.

if (FileUpload1.PostedFile.ContentLength > 0)

{

fn = Path.GetFileName(FileUpload1.FileName); // here fn will store the image filename

save = Server.MapPath(txt_foldername.Text) + “/” + fn; // making the path with created dynamically folder name

FileUpload1.SaveAs(save); // will save the image inside the dynamically created folder name

Int32 cnt;

Int32 i;

cnt = ListBox1.Items.Count; // will count the total collections in listbox

 

if (cnt == 0) // if no item is added in the listbox then it will save it and exit (return) from the code.

{ ListBox1.Items.Add(fn); return; }

//if listbox item is not empty the following code will execute.

for (i = 0; i <= cnt – 1; i++)

{

// check the filename if it is already exists in the listbox then it will not add it in.

if (ListBox1.Items[i].Text == fn)

{

Response.Write(“file name already exists”);

return;

}

}

// if not exists in the listbox then it will add it in listbox

ListBox1.Items.Add(fn);

}

}

protected void Button1_Click(object sender, EventArgs e)

{

cnt = ListBox1.Items.Count; // count the items in listbox

for (i = 0; i <= cnt – 1; i++)

{

if (ListBox1.Items[i].Selected==true)

{ //will delete the image from the dynamically created folder.

pth=(Server.MapPath(txt_foldername.Text))+“/”+ListBox1.Text;

File.Delete(pth); //deleting the file from server

ListBox1.Items.Remove(ListBox1.Text); //removing the selected item in listbox.

return;

}

}

}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)

{

cnt = ListBox1.Items.Count; // count the items in listbox

for (i = 0; i <= cnt – 1; i++)

{

if (ListBox1.Items[i].Selected == true)

{

// Will display the selected image

Image1.ImageUrl = txt_foldername.Text + “/” + ListBox1.Items[i].Text;

}}}}

 

 

With Regards

Vikram

 

Posted in Asp.net
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
« How to use ListBox and display multiple values in asp.net c#
» How to check case sensitive characters while login in asp.net C#

5 comments on “How to display image in Image control after upload on the server asp.net C#”

  1. bhagwat prasad sharma on July 29, 2011 at 1:43 pm said:

    hi all,
    image don’t show in image control ..
    i use ,,,
    Image1.ImageUrl = System.Drawing.Image.FromFile(strfn);

    what i use in Image.ImageUrl

    Reply ↓
  2. John Rob on January 6, 2012 at 8:40 am said:

    Hi,

    I was reading your article and I would like to appreciate you for making it very simple and understandable. This article gives me a basic idea of Dynamically loading image in Image control in ASP.NET and it helped me a lot.

    Thank you very much for your precious post.

    Reply ↓
  3. Dhananjay on February 14, 2012 at 12:07 pm said:

    This code is not working on my computer. What necessary other settings I have to make?

    Reply ↓
    • admin on February 18, 2012 at 5:33 am said:

      Could u let me know what kind of the error u r facing..?

      Reply ↓
  4. navneet on August 6, 2012 at 4:57 am said:

    thanks bro i really works you have really made it simple

    Reply ↓

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Joy of Helping

Any sort of help to the children who are fatherless, poor & are from far-off areas.
Joy of Helping

Facebook Link

Asp dot Net ,Ajax,Xml.

Recent Posts

  • ROW_NUMBER(), NTILE(), partition by, Duplicate Records, CTE sql server 2008
  • How to get second highest Salary sql server 2008
  • Insert values, insert into, insert default value, insert execute and select into sql server 2008
  • How to Bind GridView with SqlDataReader in Asp.net
  • how to attach files to email without storing on disk using Asp.net FileUpload control

Recent Comments

  • Anonymous on ROW_NUMBER(), NTILE(), partition by, Duplicate Records, CTE sql server 2008
  • dhananjay on How to use GridView with Insert, Edit, Update, Delete the Ado.net way C#
  • dev on How can we limit the characters in multiline textbox asp.net using JavaScript
  • navneet on How to display image in Image control after upload on the server asp.net C#
  • AnhVu on How to do Shopping Cart in Asp.net C#

Archives

  • February 2013
  • September 2012
  • April 2012
  • January 2012
  • October 2011
  • August 2011
  • May 2011
  • April 2011
  • March 2011

Categories

  • Asp.net
  • jQuery
  • Sql Server

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

EvoLve theme by Theme4Press  •  Powered by WordPress How to use Asp.net